LINQ - 实体:一次查询中的多个Sum

时间:2013-12-06 09:26:29

标签: entity-framework entity-framework-4 linq-to-entities

我有三个不同的查询来计算订单实体的每日,每周和每月总计:

DateTime sevenDaysAgo = DateTime.Today.Date.AddDays(-7);
todayT = (from c in db.OrderBills.Where(i => EntityFunctions.TruncateTime(DateTime.Today) == EntityFunctions.TruncateTime(i.BillDate))
                          select c.GrandTotal).Sum() ?? 0;

weekT = (from c in db.OrderBills.Where(i => (EntityFunctions.TruncateTime(i.BillDate) >= EntityFunctions.TruncateTime(sevenDaysAgo))
                    && (EntityFunctions.TruncateTime(i.BillDate) <= EntityFunctions.TruncateTime(DateTime.Today)))
                         select c.GrandTotal).Sum() ?? 0;

monthT = (from c in db.OrderBills.Where(i => DateTime.Today.Month == i.BillDate.Value.Month)
                          select c.GrandTotal).Sum() ?? 0;

我的问题:有没有办法将这些查询合二为一。?

编辑@Raphael

var query2 = db.OrderBills.Select(m => new
            {
                today = m.Where(i => EntityFunctions.TruncateTime(DateTime.Today) == EntityFunctions.TruncateTime(i.BillDate))
                         .Sum(i => (int?)i.GrandTotal) ?? 0
            });

0 个答案:

没有答案