gpt4 book ai didi

c# - 与 Linq 的不同日期

转载 作者:太空狗 更新时间:2023-10-29 22:53:14 26 4
gpt4 key购买 nike

我有一个名为 Billings 的实体,其属性为 DateTime。我需要唯一地找到日期,以便我可以浏览它们并做一些事情。我试过:

var DistinctYearMonthsDays = (from t in db.Billings 
where t.DateTime.HasValue
select t.DateTime.Value.Date).Distinct();

foreach (var day in DistinctYearMonthsDays)

但我得到(在 foreach 上):

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

搜索后我也尝试了以下但没有成功:

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));

非常感谢任何帮助。

最佳答案

您可以使用内置的 EntityFunctions。您的查询将变为:

var DistinctYearMonthsDays = (from t in db.Billings 
where t.DateTime.HasValue
select EntityFunctions.TruncateTime(t.DateTime)).Distinct();

有关 EntityFunctions 的更多信息,请查看 MSDN .

关于c# - 与 Linq 的不同日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11524853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com