使用LINQ to Entity framework时,Where子句中的谓词将转换为SQL。您会收到此错误,因为没有对SQL
DateTime.Add()有意义的转换。
一种快速的解决方法是将第一个Where语句的结果读入内存,然后使用LINQ to Objects完成过滤:
Context.Article.Where(p => p.StartDate < DateTime.Now) .ToList() .Where(p => p.StartDate.AddDays(p.Period) > DateTime.Now);
如果您使用的是.NET 4.0,也可以尝试EntityFunctions.AddDays方法:
Context.Article.Where(p => p.StartDate < DateTime.Now) .Where(p => EntityFunctions.AddDays(p.StartDate, p.Period) > DateTime.Now);
注意:
EF6现在
System.Data.Entity.DbFunctions.AddDays。



