Язык программирования C#9 и платформа .NET5 - Троелсен Эндрю
На нашем сайте KnigaRead.com Вы можете абсолютно бесплатно читать книгу онлайн Троелсен Эндрю, "Язык программирования C#9 и платформа .NET5" бесплатно, без регистрации.
.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()
Aztec
BMW
static 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()
Yugo
Aztec
BMW
Saab
static 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()
Yugo
Aztec
BMW
Saab
Aztec
static 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);
}