LINQ to Entities无法识别方法ToDateTime(System.String)

Dev*_*evT 4 c# linq datetime entity-framework

我必须使用"convert to 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
})
Run Code Online (Sandbox Code Playgroud)

我知道Convert.ToDateTime无法从提供者的sql中翻译,我需要一个想法来做那个查询.

更新:我不会在转换之前实现查询,因为这显然意味着获取所有记录.

Dev*_*evT 5

我想我找到了做到这一点的方法

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
})
Run Code Online (Sandbox Code Playgroud)