例如表中有列
id, 主键
SaleCount, 数量
Date, 日期
sqlserver 这样查询 可以按照 年月汇总
select SUM(SaleCount) from PreSales group by Month(Date)
select SUM(SaleCount) from PreSales group by Month(Date)
用 Linq to EF 如何 实现
EFEntities ef = new EFEntities(); var ss = from i in ef.视图固定资产转移信息 group i by i.申请时间.Year into g select new { count = g.Count(), year = g.Key }; StringBuilder sb = new StringBuilder(); foreach (var item in ss) { sb.AppendLine("年份"+item.year+"数量:"+item.count); } Response.Write(sb.ToString());
DateTime的大部分日期处理方法。在EF中都是可用的。
var data = from pre in ef.PreSale where pre.StudentID == 1 group pre by pre.Date.Month into dateGroup select new { Date = dateGroup.Select(x => x.Date).FirstOrDefault(), Month = dateGroup.Key, SaleCount = dateGroup.Sum(x => x.SaleCount) }; foreach (var v in data) { var date = v.Date; var month = v.Month; var saleCount = v.SaleCount; }