Dùng để tạo ra các giá trị mới.

Toán tử thuộc nhóm này gồm:

Toán tử Mô tả Cú pháp C# Cú pháp VB
DefaultIfEmpty Tạo ra một phần tử mặc định nếu tập dữ liệu là rỗng. Chưa hỗ trợ Chưa hỗ trợ
Empty Trả về tập dữ liệu rỗng Chưa hỗ trợ Chưa hỗ trợ
Range Phát sinh tập dữ liệu chứa số nguyên hay số Chưa hỗ trợ Chưa hỗ trợ
Repeat Phát sinh tập dữ liệu chứa các phần tử lặp lại Chưa hỗ trợ Chưa hỗ trợ

 

Ví dụ DefaultEmpty – Enumerable.DefaultEmpty.Method (C#)


void Main()

{

  Pet barley = new Pet() { Name = "Barley", Age = 4 };

  Pet boots = new Pet() { Name = "Boots", Age = 1 };

  Pet whiskers = new Pet() { Name = "Whiskers", Age = 6 };

  Pet bluemoon = new Pet() { Name = "Blue Moon", Age = 9 };

  Pet daisy = new Pet() { Name = "Daisy", Age = 3 };

  List<Pet> pets = new List<Pet>() { barley, boots, 

        whiskers, bluemoon, daisy };

  foreach (var e in pets.DefaultIfEmpty())

  {

   Console.WriteLine("Name = {0} ", e.Name);

  }

}

// Define other methods and classes here

class Pet

{

  public string Name { get; set; }

  public int Age { get; set; }

}

Ví dụ DefaultEmpty – Enumerable.DefaultEmpty.Method (VB)


Sub Main

  Dim barley As New Pet With {.Name = "Barley", .Age = 4}

  Dim boots As New Pet With {.Name = "Boots", .Age = 1}

  Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}

  Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}

  Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

  Dim pets As New System.Collections.Generic.List(Of Pet)(New Pet() 

         {barley, boots, whiskers, bluemoon, daisy})

  For Each e In pets.DefaultIfEmpty()

    Console.WriteLine("Name = {0}", e.Name)

  Next

End Sub

Class Pet

  Public Property Name As String

  Public Property Age As Integer

End Clas

Kết quả:

Ví dụ Range – Enumerable.Range.Method (C#)


//sinh ra các số từ 1 đến 5 và hiển thị bình phương các số đó

IEnumerable<int> squares = Enumerable.Range(1, 5).Select(x => x * x);

foreach (int num in squares)

{

  Console.WriteLine(num);

}

Ví dụ Range – Enumerable.Range.Method (VB)


'sinh ra các số từ 1 đến 5 và hiển thị bình phương các số đó

Dim squares As IEnumerable(Of Integer) = Enumerable.Range(1, 5).

    Select(Function(x) x * x)

Dim output As New System.Text.StringBuilder

For Each num As Integer In squares

  output.AppendLine(num)

  Console.WriteLine(num)

Next

Kết quả:

Ví dụ Repeat – Enumerable.Repeat(Of TResult).Method (C#)


IEnumerable<string> strings = Enumerable.Repeat("I like programming.", 3);

foreach (String str in strings)

{

  Console.WriteLine(str);

}

Ví dụ Repeat – Enumerable.Repeat(Of TResult).Method (VB)


Dim sentences As IEnumerable(Of String) = Enumerable.Repeat(

          "I like programming.", 3)

Dim output As New System.Text.StringBuilder

For Each sentence As String In sentences

  output.AppendLine(sentence)

  Console.WriteLine(sentence)

Next

Kết quả:

< Các toán tử truy vấn