[英]GroupBy with Where clause LINQ query
I have a question, I want to use Group by with Where clause.我有一个问题,我想将 Group by 与 Where 子句一起使用。 Here is the scenario, I want get all the orders of a user and group by them with ordernumber so it doesn't show me multiple orders with same order number.
这是场景,我想获取用户的所有订单并使用 ordernumber 按它们分组,因此它不会向我显示具有相同订单号的多个订单。 I tried to use LINQ query but that doesn't seemed to work.
我尝试使用 LINQ 查询,但这似乎不起作用。 Does anyone have any idea?
有人有什么主意吗?
var result = Entity.Where(x => x.UserId == Guid.Parse(userId)).GroupBy(x => x.OrderNumber).ToList();
This is what I tried.这是我尝试过的。
You can get first record from group.您可以从组中获取第一条记录。
var result = Entity
.Where(x => x.UserId == Guid.Parse(userId))
.GroupBy(x => x.OrderNumber)
.Select(g => g.First())
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.