我试图在月份和年份对实体结果进行分组而没有成功。
表格
当前结果:
[
{
"storeName": "Stockholm",
"values": [
{
"year": 2014,
"month": "januari",
"orderCount": 28473
}
]
},
{
"storeName": "Stockholm",
"values": [
{
"year": 2014,
"month": "februari",
"orderCount": 26830
}
]
}
]
希望的结果
[
{
"storeName": "Stockholm",
"values": [
{
"year": 2014,
"month": "januari",
"orderCount": 28473
},
{
"year": 2014,
"month": "februari",
"orderCount": 26830
}
]
}
]
代码:(这是处理分组的代码)
public static IEnumerable<MonthlyOrdersByStoreResponse> ToMonthlyOrdersByStore(this IEnumerable<OrderMonthlyEntity> orderMonthlyEntities)
{
return from order in orderMonthlyEntities
group order by new { order.StoreName, order.Month } into grouping
select
new MonthlyOrdersByStoreResponse()
{
StoreName = grouping.Key.StoreName,
Values = (from q in grouping
select
new OrderDeliveryCountByMonth()
{
Year = q.Year,
Month = new DateTime(q.Year, q.Month, 1).ToMonthName(),
OrderCount = q.OrderCount,
}).ToList()
};
}
答案 0 :(得分:0)
解决了此代码的问题:
public static IEnumerable<MonthlyOrdersByStoreResponse> ToMonthlyOrdersByStore(this IEnumerable<OrderMonthlyEntity> orderMonthlyEntities)
{
return from order in orderMonthlyEntities
group order by new { order.StoreName } into grouping
select
new MonthlyOrdersByStoreResponse()
{
StoreName = grouping.Key.StoreName,
Values = (from q in grouping
select
new OrderDeliveryCountByMonth()
{
Year = q.Year,
Month = new DateTime(q.Year, q.Month, 1).ToMonthName(),
OrderCount = q.OrderCount,
}).ToList()
};
}
答案 1 :(得分:0)
您希望结果按StoreName
分组,然后按Year, Month
分组:
return
from item in orderMonthlyEntities
group item by item.StoreName into storeNameGroup
orderby storeNameGroup.Key
select new MonthlyOrdersByStoreResponse
{
StoreName = storeNameGroup.Key,
Values =
(from item in storeNameGroup
group item by new { item.Year, item.Month } into monthGroup
orderby monthGroup.Key.Year, monthGroup.Key.Month
select new OrderDeliveryCountByMonth
{
Year = monthGroup.Key.Year,
Month = new DateTime(monthGroup.Key.Year, monthGroup.Key.Month, 1).ToMonthName(),
OrderCount = monthGroup.Sum(item => item.OrderCount)
}).ToList()
};