[英]LINQ to Entities does not recognize the method ToDateTime(System.String)
I have to use a "convert to datetime" method to compare the datetime with the reference month and year, as below 我必须使用“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
})
I know that Convert.ToDateTime can't be translated in sql from the provider, I need an idea to do that query. 我知道Convert.ToDateTime无法从提供者的sql中翻译,我需要一个想法来做那个查询。
UPDATE: I wouldn't materialize the query before the Convert, because that obviously means takes all the records. 更新:我不会在转换之前实现查询,因为这显然意味着获取所有记录。
I Think I found the way to do it 我想我找到了做到这一点的方法
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
})
It's a little crude but you can materialise the SQL query to list before you parse the date: 它有点粗糙,但您可以在解析日期之前实现要查找的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
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.