LINQ to Entities group-by 失败使用 .date [英] LINQ to Entities group-by failure using .date
本文介绍了LINQ to Entities group-by 失败使用 .date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据日期时间字段的日期部分创建一个 Linq 组.
I am trying to do a Linq group by on just the date part of a datetime field.
此 linq 语句有效,但它按日期和时间分组.
This linq statement works but it groups by the date and the time.
var myQuery = from p in dbContext.Trends
group p by p.UpdateDateTime into g
select new { k = g.Key, ud = g.Max(p => p.Amount) };
当我运行此语句以仅按日期分组时,出现以下错误
When I run this statement to group by just the date I get the following error
var myQuery = from p in dbContext.Trends
group p by p.UpdateDateTime.Date into g //Added .Date on this line
select new { k = g.Key, ud = g.Max(p => p.Amount) };
LINQ to Entities 不支持指定的类型成员日期".仅支持初始值设定项、实体成员和实体导航属性.
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
如何根据日期而不是日期和时间进行分组?
How can I do a group by for the date and not the date and time?
推荐答案
使用 EntityFunctions.TruncateTime 方法:
var myQuery = from p in dbContext.Trends
group p by EntityFunctions.TruncateTime(p.UpdateDateTime) into g
select new { k = g.Key, ud = g.Max(p => p.Amount) };
这篇关于LINQ to Entities group-by 失败使用 .date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文