我有以下数据:
Type (enum) Date (DateTime) Count (int)
Red 2014-07-27 11:00:00 1
Red 2014-07-27 10:00:00 1
Red 2014-07-27 09:00:00 1
Blue 2014-07-27 11:00:00 1
Blue 2014-07-27 10:00:00 1
Blue 2014-07-27 09:00:00 1
我想先按进行分组Type
,然后Count
对每天进行汇总。
我想要的输出将是:
Type (enum) Date (DateTime) Count (int)
Red 2014-07-27 3
Blue 2014-07-27 3
以下代码将按我的意愿按天分组,但是我不知道如何将byType
和by组合在一起Date
:
_entityContext.Statistics.
.GroupBy(s => new { s.DateTime.Year, s.DateTime.Month, s.DateTime.Day})
我已经为此苦苦挣扎了一段时间,最终遇到了复杂的IGrouping结构,现在有点卡住了。Google引导我转向查询语法结构,但我真的很想知道使用方法语法是否可以实现。最后我知道查询语法已转换为方法语法,因此应该可行吗?
任何人都可以指引我正确的方向吗?
注意: LINQ TO实体不支持访问“ DateTime”上的“ Date”属性。您将得到的异常是:“ LINQ to Entities不支持指定的类型成员'Date'。仅支持初始化程序,实体成员和实体导航属性”
只需添加Type
到您的GroupBy
:
_entityContext.Statistics.GroupBy(
// Define the key for the GroupBy to be Type and the Day
s => new { s.Type, s.Date.Year, s.Date.Month, s.Date.Day},
// Reduce each group to just the key and the sum of its Count values
(key, ss) => new { key, count = ss.Sum(s => s.Count) }
);
经测试可与LINQ-to-Entities一起使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句