我有类人
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string VisitPlace { get; set; }
}
我有这个班的名单
List<Person> list = new List<Person>()
{
new Person { FirstName = "Name1", LastName = "Smith", VisitPlace = "London; Paris" },
new Person { FirstName = "Name2", LastName = "Smith", VisitPlace = "Berlin" },
new Person { FirstName = "Name3", LastName = "Smith", VisitPlace = "London; Berlin" },
new Person { FirstName = "Name4", LastName = "Smith", VisitPlace = "Berlin" },
new Person { FirstName = "Name5", LastName = "Smith", VisitPlace = null },
new Person { FirstName = "Name6", LastName = "Smith", VisitPlace = "Paris" },
new Person { FirstName = "Name7", LastName = "Smith", VisitPlace = null },
new Person { FirstName = "Name8", LastName = "Smith", VisitPlace = "Paris; London" },
new Person { FirstName = "Name9", LastName = "Smith", VisitPlace = "London" },
};
我想对这个列表进行排序:
- 参观伦敦广场的人
- 参观巴黎广场的人
- 访问柏林的人(如果一个人有两个访问地点,例如(访问地点=伦敦;巴黎)那么重要的只是第一名)
- visitPlace = null的所有人员-位于列表末尾
var list2 = list.OrderByDescending(x => x.VisitPlace).ThenByDescending(x => x.VisitPlace == "London").ThenBy(x => x.VisitPlace == "Paris").ThenBy(x => x.VisitPlace == "Berlin");
更正列表中的人员:人员1、人员3、人员9、人员6、人员8、人员2、人员4、人员5、人员7
它是排序的,但只是orderbyDescending。然后By不起作用。
为什么thenBy不起作用?我怎样才能用linq正确地排序呢?
3条答案
按热度按时间2ledvvac1#
这对我很有效:
idfiyjo82#
这对我很有效:
它给出:
更可靠的版本如下:
s8vozzvw3#
这对我很有效:
我将把逻辑移到一个专用的方法中,如下所示:
在这里,您可以看到一个有效的解决方案:
https://dotnetfiddle.net/50kYS2