使用 LINQ 在列表中进行 Groupby 列表
我有两个类:
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,我想按邮政编码获取订单组列表。如果邮政编码“12121”有 10 个客户,每个客户有 2 个订单,那么它应该只返回一个包含 20 个订单列表的邮政编码。
我正在尝试这样做,但无法弄清楚出了什么问题,
var orders = br.CustOrderList
.Select(r => new
{
r.ZipCode,
r.Name,
r.OrderList
})
.GroupBy(x => new { x.ZipCode, x.OrderList});
有什么帮助吗?
I have two classes:
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; }
}
Using LINQ, i want to get list of Orders group by ZipCode. If Zipcode "12121" has 10 customers and each has 2 orders then it should return me only one Zipcode with the list of 20 orders.
I am trying to do it like this but not able to figure out whats wrong
var orders = br.CustOrderList
.Select(r => new
{
r.ZipCode,
r.Name,
r.OrderList
})
.GroupBy(x => new { x.ZipCode, x.OrderList});
Any help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该做你想要的:
This should do what you want:
您只想按邮政编码分组,所以只需按邮政编码分组
啊是的,只需尝试即可
,无需从列表中选择新项目
You just want to group by the ZipCode so just group by that
Ah yea just try
No need to select new items out of the list