我的模型是:
class Person
{
public int Id {get; set; }
public string Name {get; set; }
}
我有两个集合。我希望像 etalonList
一样对 toBeSortedList
进行排序:
List<Person> etalonList = new List<Person>()
{
new Person() { Id=10, Name="Jon"},
new Person() { Id=4, Name="Ben"},
new Person() { Id=11, Name="Magnus"},
new Person() { Id=8, Name="Joseph"},
};
List<Person> toBeSortedList = new List<Person>()
{
new Person() { Id=11, Name="Magnus"},
new Person() { Id=4, Name="Ben"},
new Person() { Id=10, Name="Jon"},
new Person() { Id=8, Name="Joseph"},
};
我尝试过:
var orderedByIdList = tobeSortedList.OrderBy(x => etalonList.IndexOf(x.Id));
但我遇到了这样的错误:
也许你有其他建议?无法将“int”转换为“SOConsoleApplication.Person”
ToList
并不能修复错误。 - DavidG