PiZ*_*zL3 3 c# linq sorting lambda
我将如何通过在 Person 中选择的字典值对其进行排序?
示例:按“汽车”值降序排列“人”的顺序列表。
用那些奇特的 lambda/linq 方程之一可以做到吗?
public class People
{
List<Person> Persons { get; set; }
}
public class Person
{
public Dictionary<string, string> cars { get; set; }
public Dictionary<string, string> houses { get; set; }
public Dictionary<string, string> banks { get; set; }
}
................
People people = new People();
people.Persons = new List<Person>
{
new Person
{
cars = new Dictionary<string, string> { { "volvo", "340050" }, { "bmw", "50545000" } },
houses = new Dictionary<string, string> { { "mansion", "100040000" },{ "cardboard box", "112" } },
banks = new Dictionary<string, string> { { "swiss", "12500330000" }, { "camen", "12000000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "34023200" }, { "bmw", "5003300" } },
houses = new Dictionary<string, string> { { "mansion", "1000330000" },{ "cardboard box", "277" } },
banks = new Dictionary<string, string> { { "swiss", "12500044000" }, { "camen", "12000000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "3554000" }, { "bmw", "50023200" } },
houses = new Dictionary<string, string> { { "mansion", "1006600000" },{ "cardboard box", "244" } },
banks = new Dictionary<string, string> { { "swiss", "125000544000" }, { "camen", "12000777000" } }
}
};
Run Code Online (Sandbox Code Playgroud)
编辑/示例结果:
结果将是基于每个 Person 字典中包含的值的新列表或重新排序的列表
人 -> 汽车 -> { "volvo", "34023200" }
人 -> 汽车 -> { "volvo", "3554000" }
人 -> 汽车 -> { "volvo", "340050" }
新的或重新排序的 List 将如下所示:
people.Persons = new List<Person>
{
new Person
{
cars = new Dictionary<string, string> { { "volvo", "34023200" }, { "bmw", "5003300" } },
houses = new Dictionary<string, string> { { "mansion", "1000330000" },{ "cardboard box", "277" } },
banks = new Dictionary<string, string> { { "swiss", "12500044000" }, { "camen", "12000000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "3554000" }, { "bmw", "50023200" } },
houses = new Dictionary<string, string> { { "mansion", "1006600000" },{ "cardboard box", "244" } },
banks = new Dictionary<string, string> { { "swiss", "125000544000" }, { "camen", "12000777000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "340050" }, { "bmw", "50545000" } },
houses = new Dictionary<string, string> { { "mansion", "100040000" },{ "cardboard box", "112" } },
banks = new Dictionary<string, string> { { "swiss", "12500330000" }, { "camen", "12000000" } }
}
};
Run Code Online (Sandbox Code Playgroud)
这是一个绝妙的技巧;基本上,可以将 Dictionary 视为 KeyValuePair<> 对象的列表。
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
foreach(var item in myDictionary)
// item is a KeyValuePair<int, string>, enumerated in order of their insertion into the dictionary
Run Code Online (Sandbox Code Playgroud)
现在它在一个列表中,很容易通过 LINQ 进行排序:
Dictionary<int, string> sample = new Dictionary<int, string>
{
{ 1, "Sample" },
{ 2, "Another Sample" }
};
var orderedList = sample.OrderBy(x => x.Value).ToList();
Run Code Online (Sandbox Code Playgroud)
当然,更大的问题是为什么您需要对字典进行排序和排序 - 这并不是真正与字典概念分组的操作(它更多是为了通过指定的键快速访问特定元素)。你写的问题描述看起来很奇怪;您可以为特定的人订购汽车,但是您是否试图从每个订购的人中按价值查找每辆车?
这也很容易做到:
List<PersonCarDetails> allStuff = new List<PersonCarDetails>();
foreach(var person in persons)
allStuff.AddRange(person.Cars.Select(x => new PersonCarDetails { PersonId = person.Id, CarName = x.Key, CarId = x.Value }).ToList());
// allStuff now contains a list of PersonCarDetails, and then you can order that:
var orderedList = allStuff.OrderBy(x => x.CarId).ToList();
Run Code Online (Sandbox Code Playgroud)