use*_*996 5 c# linq datetime linq-to-entities distinct
我有一个名为Billings的实体,其属性为DateTime.我需要独特地找到日期,以便我可以浏览它们并做一些事情.我试过了:
var DistinctYearMonthsDays = (from t in db.Billings
where t.DateTime.HasValue
select t.DateTime.Value.Date).Distinct();
foreach (var day in DistinctYearMonthsDays)
Run Code Online (Sandbox Code Playgroud)
但我得到(上foreach):
LINQ to Entities不支持指定的类型成员"Date".仅支持初始值设定项,实体成员和实体导航属性.
搜索后我也尝试了以下但没有成功:
IEnumerable<DateTime> DistinctYearMonthsDays = db.Billings
.Select(p => new
{
p.DateTime.Value.Date.Year,
p.DateTime.Value.Date.Month,
p.DateTime.Value.Date.Day
}).Distinct()
.ToList()
.Select(x => new DateTime(x.Year,x.Month,x.Day));
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感谢.
您可以使用内置的EntityFunctions.您的查询将变为:
var DistinctYearMonthsDays = (from t in db.Billings
where t.DateTime.HasValue
select EntityFunctions.TruncateTime(t.DateTime)).Distinct();
Run Code Online (Sandbox Code Playgroud)
有关EntityFunctions的更多信息,请查看MSDN.
| 归档时间: |
|
| 查看次数: |
2840 次 |
| 最近记录: |