C# LINQ to实体无法识别方法ToDateTime(System.String)

C# LINQ to实体无法识别方法ToDateTime(System.String),c#,entity-framework,linq,datetime,C#,Entity Framework,Linq,Datetime,我必须使用“convert to datetime”方法将datetime与参考月份和年份进行比较,如下所示 var res = db.Table1 //other tables, and where conditions .Select(x => new MyObj { //... Imp = x.MyTable.FirstOrDefault(y => y.date_val >= System.Data.Entity.DbFunctions.AddM

我必须使用“convert to datetime”方法将datetime与参考月份和年份进行比较,如下所示

var res =  db.Table1
//other tables, and where conditions
.Select(x => new MyObj
{
      //...
      Imp = x.MyTable.FirstOrDefault(y => y.date_val >= System.Data.Entity.DbFunctions.AddMonths(Convert.ToDateTime("01/" + x.month + "/" + x.year), 1))).Description
})
我知道Convert.ToDateTime无法从提供程序转换为sql,我需要一个方法来执行该查询


更新:我不会在转换之前具体化查询,因为这显然意味着需要所有记录。

这有点粗糙,但您可以在解析日期之前将SQL查询具体化为列表:

var res = db.Table1.ToList()
//other tables, and where conditions
.Select(x => new MyObj
{
//...
    Imp = x.MyTable.FirstOrDefault(y => y.date_val >= System.Data.Entity.DbFunctions.AddMonths(DateTime.Parse("01/" + x.month + "/" + x.year))).Description
})

我想我找到了办法

var res =  db.Table1
//other tables, and where conditions
.Select(x => new MyObj
{
      //...
      Imp = x.MyTable.FirstOrDefault(y => y.date_val >= System.Data.Entity.DbFunctions.AddMonths(System.Data.Entity.DbFunctions.CreateDateTime(x.year, x.month, 1, 0, 0, 0), 1)).Description
})

Convert.ToDateTime
无法转换为t-SQL,Linq to Entities无法识别它。看看这个答案:@S.Akbari是的,我知道,但我需要一个变通方法或类似的方法,尝试使用一个numerable()强制使用Linq对该零件求值到对象。因此,这将变成:
x.MyTable.AsEnumerable().FirstOrDefault……
。。。只是想一想,我还没有用日期转换来测试这个问题。@Spluf仍然将
MyTable
的全部内容带到内存中,这通常是不可取的。如果可能的话,我以前不会具体化查询。