我有一个场景,需要先按组分组,然后对每个组进行排序。我想出了以下可行的查询:
applicants = context.Applicants
.OrderBy(app => app.Id).Skip((pageNumber - 1)*defaultPageSize)
.Take(defaultPageSize)
.GroupBy(app => app.PassportNumber)
.Select(g => g.OrderBy(d => d.IsEndorseChild)).ToList()
.SelectMany(grouping => grouping)
.ToList();
但是这并不起作用:
applicants = context.Applicants
.OrderBy(app => app.Id).Skip((pageNumber - 1)*defaultPageSize)
.Take(defaultPageSize)
.GroupBy(app => app.PassportNumber)
.Select(g => g.OrderBy(d => d.IsEndorseChild))
.SelectMany(grouping => grouping)
.ToList();
第一个查询返回了申请人的Id,结果如下:
7, 10, 8, 2, 9, 6, 5, 3, 4, 1
但是第二个查询返回了:
7, 10, 8, 2, 9, 6, 3, 4, 5, 1
我不确定为什么会出现这种情况,两个查询之间有什么区别。
我到底错在哪里了吗?
编辑: 示例输入和期望输出:
Applicant 1 => ID : 1
IsEndorsed : 0
Passport : ABC123
Applicant 2 => ID : 2
IsEndorsed : 1
Passport : ABC123
Applicant 3 => ID : 3
IsEndorsed : 0
Passport : ABC1234
Applicant 4 => ID : 4
IsEndorsed : 0
Passport : A1234
Applicant 5 => ID : 5
IsEndorsed : 1
Passport : PQR123
Applicant 6 => ID : 6
IsEndorsed : 1
Passport : PQR123
Applicant 7 => ID : 7
IsEndorsed : 0
Passport : PQR123
Expected output : (Grp by Passport Number and in each group IsEndorsed = 0 is at top)
----------------------------------------------
Applicant 1 => ID : 1
IsEndorsed : 0
Passport : ABC123
Applicant 2 => ID : 2
IsEndorsed : 1
Passport : ABC123
Applicant 3 => ID : 3
IsEndorsed : 0
Passport : ABC1234
Applicant 4 => ID : 4
IsEndorsed : 0
Passport : AX1234
Applicant 7 => ID : 7
IsEndorsed : 0
Passport : PQR123
Applicant 5 => ID : 5
IsEndorsed : 1
Passport : PQR123
Applicant 6 => ID : 6
IsEndorsed : 1
Passport : PQR123