gpt4 book ai didi

c# - 在 Linq 中按 DayOfWeek 分组

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:47 24 4
gpt4 key购买 nike

我有这个 Linq 查询,它工作正常

var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by c.Date.Day into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()

}).ToList();

但我想通过 DayOfWeek 属性对这些结果进行分组,Linq 似乎不允许这样做,因为当我用 c.Date 替换 group c 时出现此错误。 c.Date.DayOfWeek 的 c 组 的 Day :LINQ to Entities 不支持指定的类型成员“DayOfWeek”

这个问题有解决办法吗?

最佳答案

使用SqlFunctions.DatePart :

var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by SqlFunctions.DatePart("weekday", c.Date) into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()

}).ToList();

关于c# - 在 Linq 中按 DayOfWeek 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15907394/

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