Язык программирования C#9 и платформа .NET5 - Джепикс Филипп - Страница 266
- Предыдущая
- 266/642
- Следующая
Изменить размер шрифта:
266
.Except(from c2 in yourCars select c2); Console.WriteLine("Here is what you don't have, but I do:"); foreach (string s in carDiff) { Console.WriteLine(s); // Выводит Yugo. }}Метод
Intersect()AztecBMWstatic void DisplayIntersection(){ List<string> myCars = new List<String> { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<String> { "BMW", "Saab", "Aztec" }; // Получить общие члены. var carIntersect = (from c in myCars select c) .Intersect(from c2 in yourCars select c2); Console.WriteLine("Here is what we have in common:"); foreach (string s in carIntersect) { Console.WriteLine(s); // Выводит Aztec и BMW. }}Метод
Union()YugoAztecBMWSaabstatic void DisplayUnion(){ List<string> myCars = new List<string> { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<String> { "BMW", "Saab", "Aztec" }; // Получить объединение двух контейнеров. var carUnion = (from c in myCars select c) .Union(from c2 in yourCars select c2); Console.WriteLine("Here is everything:"); foreach (string s in carUnion) { Console.WriteLine(s); // Выводит все общие члены. }}Наконец, расширяющий метод
Concat()YugoAztecBMWSaabAztecstatic void DisplayConcat(){ List<string> myCars = new List<String> { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<String> { "BMW", "Saab", "Aztec" }; var carConcat = (from c in myCars select c) .Concat(from c2 in yourCars select c2); // Выводит Yugo Aztec BMW BMW Saab Aztec. foreach (string s in carConcat) { Console.WriteLine(s); }}Устранение дубликатов
При вызове расширяющего метода
Concat()Distinct()static void DisplayConcatNoDups(){ List<string> myCars = new List<String> { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<String> { "BMW", "Saab", "Aztec" }; var carConcat = (from c in myCars select c) .Concat(from c2 in yourCars select c2); // Выводит Yugo Aztec BMW Saab. foreach (string s in carConcat.Distinct()) { Console.WriteLine(s); }
266
- Предыдущая
- 266/642
- Следующая
Перейти на страницу:
