Cho phép kết hợp hai nguồn dữ liệu thành một nguồn duy nhất (giống phép hợp (union) hai tập hợp nhưng có sự lặp lại các thành phần giống nhau).
Toán tử thuộc nhóm này gồm:
| Toán tử | Mô tả | Cú pháp C# | Cú pháp VB |
| Concat | Kết hợp hai nguồn dữ liệu thành một nguồn duy nhất. | Chưa hỗ trợ | Chưa hỗ trợ |
Ví dụ C#:
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();
IEnumerable<string> query = cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));
foreach (var e in query)
{
Console.WriteLine("Name = {0} ", e);
}
static Pet[] GetCats()
{
Pet[] cats = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
return cats;
}
static Pet[] GetDogs()
{
Pet[] dogs = { new Pet { Name="Bounder", Age=3 },
new Pet { Name="Snoopy", Age=14 },
new Pet { Name="Fido", Age=9 } };
return dogs;
}
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Ví dụ trong VB:
Dim cats As List(Of Pet) = GetCats()
Dim dogs As List(Of Pet) = GetDogs()
Dim list = cats.Cast(Of Pet)().Concat(dogs.Cast(Of Pet)()).ToList()
For Each e In list
Console.WriteLine("Name = {0}", e.Name)
Next
Function GetCats() As List(Of Pet)
Dim cats As New List(Of Pet)
cats.Add(New Pet With {.Name = "Barley", .Age = 8})
cats.Add(New Pet With {.Name = "Boots", .Age = 4})
cats.Add(New Pet With {.Name = "Whiskers", .Age = 1})
Return cats
End Function
Function GetDogs() As List(Of Pet)
Dim dogs As New List(Of Pet)
dogs.Add(New Pet With {.Name = "Bounder", .Age = 3})
dogs.Add(New Pet With {.Name = "Snoopy", .Age = 14})
dogs.Add(New Pet With {.Name = "Fido", .Age = 9})
Return dogs
End Function
Class Pet
Public Property Name As String
Public Property Age As Integer
End Class
Kết quả trong LINQPad
