我的结构相当简单:
每位客户都有 WeeklyOrder
WeeklyOrder 包含5个 DailyOrders 和
在每个 DailyOrder 中有5个 OrderItems , ItemType (基本上是5个广告位之一)和计数。
我想得到本周的重要性。 所以本周我得到了所有用户的所有 DailyOrders 。
var orders = db
.WeeklyOrders
.Where(x => x.WeekNumber == week && x.Year == year)
.SelectMany(x => x.DailyOrders);
一切正常,但现在我不知道如何通过 DailyOrder.DayNumber 对每日订单进行分组,以及如何对每个DailyOrder中的项目进行分组(并非所有日常订单中的项目分组)所以我知道所有用户的总和 - 星期一为1号订购了多少件物品等等。
我只要求正确的linq查询。
为了让您了解我想要在我看来的内容:
星期一
星期二
...
等
这是我的模特:
public class WeeklyOrder
{
public int WeeklyOrderId { get; set; }
public int UserId { get; set; }
public int Year { get; set; }
public int WeekNumber { get; set; }
public virtual Customer Customer { get; set; }
public virtual List<DailyOrder> DailyOrders { get; set; }
}
public class DailyOrder
{
public int DailyOrderId { get; set; }
public int DayNumber { get; set; }
public int WeeklyOrderId { get; set; }
public virtual WeeklyOrder WeeklyOrder { get; set; }
public virtual List<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
public int OrderItemId { get; set; }
public int Count { get; set; }
public int ItemTypeId { get; set; }
public int DailyOrderId { get; set; }
public virtual DailyOrder DailyOrder { get; set; }
public virtual ItemType Type { get; set; }
}
答案 0 :(得分:1)
试试这个:
var orders = db
.WeeklyOrders
.Where(x => x.WeekNumber == week && x.Year == year)
.SelectMany(x => x.DailyOrders.SelectMany(y => y.OrderItems))
.GroupBy(a => new
{
DayNumber = a.DailyOrder.DayNumber,
Type = a.Type
})
.Select(a => new
{
DayNumber = a.Key.DayNumber,
Type = a.Key.Type,
Total = a.Sum(b => b.Count)
});