我有这个:
List<string> lstRanksOrder = new List<string>() {
"Captain", "Lieutenant", "Sergeant", "Corporal Master",
"Corporal Senior", "Corporal 1", "Corporal", "Civilian Pilot" };
var emp = test
.ToList()
.Select(x => new
{
EID = x.IBM,
Description = string.Format("{0} {1}", x.FirstName, x.LastName),
Group = x.RPosition
})
.AsEnumerable()
.OrderBy(x => lstRanksOrder.IndexOf(x.Group))
.ThenBy(x => x.Description)
.Distinct()
.ToList();
ThenBy
条款工作正常,但有没有办法在LastName
之前FirstName
更改订单,而不将Description
更改为Description = string.Format("{0} {1}", x.LastName, x.FirstName)
?
答案 0 :(得分:4)
在调用Select
扩展名方法之前订购集合:
var emp = test.ToList()
.OrderBy(x => lstRanksOrder.IndexOf(x.RPosition))
.ThenBy(x => x.LastName)
.ThenBy(x => x.FirstName)
.Select(x => new
{
EID = x.IBM,
Description = string.Format("{0} {1}", x.FirstName, x.LastName),
Group = x.RPosition
})
.Distinct()
.ToList();
答案 1 :(得分:1)
Distinct
扩展方法之前, Order
然后Select
集合:
var emp = test.ToList()
.Distinct()
.OrderBy(x => lstRanksOrder.IndexOf(x.RPosition))
.ThenBy(x => x.LastName)
.ThenBy(x => x.FirstName)
.Select(x => new
{
EID = x.IBM,
Description = string.Format("{0} {1}", x.FirstName, x.LastName),
Group = x.RPosition
})
.ToList();