1

我有这个运行良好的 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 不允许这样做,因为当我替换为时出现此group c by c.Date.Day错误group c by c.Date.DayOfWeekThe specified type member 'DayOfWeek' is not supported in LINQ to Entities

这个问题有什么解决方法吗?

4

1 回答 1

6

使用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();
于 2013-04-09T16:28:30.870 回答