[英]LINQ Join Order By then Result Order By
I have a LINQ to construct a list of buildings, which has a field consisting of a list of Users: 我有一个LINQ来构建建筑物列表,其中包含一个由用户列表组成的字段:
List<Building> buildings = (from b in db.Buildings
join u in db.BuildingUsers on b.BuildingId equals u.BuildingId into bUsers
orderby b.BuildDate descending
select Building.Create(b, bUsers)).ToList();
The UserName is not always the user I want. UserName并不总是我想要的用户。 What I really want is the first User that was ever entered, which I think is correct to assume it would be the BuildingUser with the lowest UserID.
我真正想要的是有史以来输入的第一个用户,我认为假设它是具有最低UserID的BuildingUser是正确的。 So I do the Order By:
所以我的命令是:
List<Building> buildings = (from b in db.Buildings
join u in db.BuildingUsers on b.BuildingId equals u.BuildingId into bUsers
orderby u.UserId ascending
select Building.Create(b, bUsers)).ToList();
That works great, except now my overall list of buildings is in an awkward order. 效果很好,只不过现在我的整体建筑物列表处于尴尬状态。 The bUsers is passed into the Create method in the correct order, and I handle the FirstOrDefault() to get the first user and handle the rest.
bUsers以正确的顺序传递到Create方法中,我处理FirstOrDefault()以获取第一个用户并处理其余用户。 I want the end-result of the overall building list to be in order by BuildDate.
我希望整个建筑物清单的最终结果能够按BuildDate排序。
List<Building> buildings = (from b in db.Buildings
join u in db.BuildingUsers on b.BuildingId equals u.BuildingId into bUsers
orderby u.UserId ascending, b.BuildDate descending
select Building.Create(b, bUsers)).ToList();
Of course this doesn't work, because now it first sorts by UserId, then by BuildDate and doesn't quite turn out correctly. 当然,这是行不通的,因为现在它首先按UserId排序,然后按BuildDate排序,并且结果不正确。
So I want one orderby for the Join, and a different orderby for the main result. 因此,我希望对join使用一个orderby,对主要结果使用不同的orderby。 I'd rather not split them into multiple methods so I don't have to alter the way I construct my building objects.
我不想将它们拆分为多种方法,因此不必更改构建建筑对象的方式。
You were right in using orderby b.BuildDate descending
because that is the ordering expected in your output collection buildings
. 您使用
orderby b.BuildDate descending
排序是正确的,因为这是您的输出集合buildings
预期的排序。 The ordering of the subcollection bUsers
should be performed when passing it to Building.Create()
: 将子集合
bUsers
传递给Building.Create()
时,应对其进行排序:
List<Building> buildings = (from b in db.Buildings
join u in db.BuildingUsers on b.BuildingId equals u.BuildingId into bUsers
orderby b.BuildDate descending
select Building.Create(b, bUsers.OrderBy(bu => bu.UserId))).ToList();
Based on what you are saying above I might suggest changing Building.Create
to accept a single user, in which case you could perform the FirstOrDefault()
in this query: 基于以上内容,我可能建议将
Building.Create
更改为接受单个用户,在这种情况下,您可以在此查询中执行FirstOrDefault()
:
List<Building> buildings = (from b in db.Buildings
join u in db.BuildingUsers on b.BuildingId equals u.BuildingId into bUsers
orderby b.BuildDate descending
select Building.Create(b, bUsers.OrderBy(bu => bu.UserId).FirstOrDefault())).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.