我有两个班级:
class Customer
{
public string Name { get; set; }
public string ZipCode { get; set; }
public List<Order> OrderList { get; set; }
}
class Order
{
public string OrderNumber { get; set; }
}
使用LINQ,我希望通过ZipCode获取Orders组列表。如果Zipcode“12121”有10个客户,每个客户有2个订单,那么它应该只返回一个包含20个订单列表的Zipcode。
我试图像这样做,但无法弄清楚什么是错的
var orders = br.CustOrderList
.Select(r => new
{
r.ZipCode,
r.Name,
r.OrderList
})
.GroupBy(x => new { x.ZipCode, x.OrderList});
请帮忙吗?
答案 0 :(得分:6)
这应该做你想要的:
var orders = br.CustOrderList
.GroupBy(x => x.ZipCode)
.Select(g => new
{
ZipCode = g.Key,
Orders = g.SelectMany(x => x.OrderList)
});
答案 1 :(得分:2)
var orders = br.CustOrderList
.Select(r => new
{
r.ZipCode,
r.Name,
r.OrderList
})
.GroupBy(x => x.ZipCode);
您只想按ZipCode进行分组,以便按
进行分组啊,你只是试试
var order = br.CustOrderList
.GroupBy(x = x.ZipCode);
无需从列表中选择新项目