日期:2014-05-17  浏览次数:20473 次

linq 表达式问题
本帖最后由 zxy397472251 于 2013-06-08 10:41:48 编辑

    public bool ExistGspComQualify(string ComQualifyId, string CompanyId)
        {
            string sql = "select count(1)  from SysComQualify  where LicStatus>=0 and DATEDIFF(DAY,ValidStratDate,ValidUntil)>0 and ComQualifyId = '" + ComQualifyId + "' and CompanyId='" + CompanyId + "' ";
            return DBHelperSQL.Exists(sql);
        }

        //去掉 && (c.ValidUntil.Value - c.ValidStratDate.Value).Days > 0 代码才能正常
        public bool ExistGspComQualify1(string ComQualifyId, string CompanyId)
        {
            string[] licStatusNo = { "-1", "-2", "-10" };  
            var query = Context.SysComQualify.Where(c => !licStatusNo.Contains(c.LicStatus) && c.ComQualifyId == ComQualifyId && c.CompanyId == CompanyId && (c.ValidUntil.Value - c.ValidStratDate.Value).Days > 0);
            return query.Count() > 0 ? true : false;
        }


注:c.ValidUntil和c.ValidStratDate 类型为DateTime?
疑问是:第一段sql没问题,第二段 linq表达式中存在列之间操作,无法识别,你们是如何处理的?

------解决方案--------------------
 LINQ to sql的话
var query = Context.SysComQualify.Where(c => !licStatusNo.Contains(c.LicStatus) && c.ComQualifyId == ComQualifyId && c.CompanyId == CompanyId && System.Data.Linq.SqlClient.SqlMethods.DateDiffDay(c.ValidUntil.Value,c.ValidStratDate.Value)>0);

如果是entity framework
用System.Data.Objects.SqlClient.SqlFunctions.DateDiff("day", c.ValidUntil.Value,c.ValidStratDate.Value) >0
------解决方案--------------------
如果是LINQ TO SQL:

System.Data.Linq.SqlClient.SqlMethods.DateDiffDay( c.ValidUntil.Value,c.ValidStratDate.Value)