LINQ生成操作


新序列的值是由生成運算子建立的。

操作 描述 C#查詢表示式語法 VB查詢表示式語法
預設空 當應用於一個空的序列,產生一個序列內的預設元素 不適用 不適用
返回值是空序列,是最簡單的代操作符 不適用 不適用
範圍 產生具有整數或數位序列的集合 不適用 不適用
重複 生成包含特定長度的重複值的序列 不適用 不適用

DefaultIfEmpty例子 - Enumerable.DefaultIfEmpty.Method

C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace Operators
{
  class DefaultEmpty
  {
     static void Main(string[] args)
     {
        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);
        }

        Console.WriteLine("\nPress any key to continue.");
        Console.ReadKey();
     }

     class Pet
     {
        public string Name { get; set; }
        public int Age { get; set; }
     }
  }
}

VB

Module Module1

  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

     Console.WriteLine(vbLf & "Press any key to continue.")
     Console.ReadKey()
  End Sub

  Class Pet
     Public Property Name As String
     Public Property Age As Integer
  End Class
End Module

當C#或VB的上述程式碼被編譯和執行時,它產生了以下結果:

Name = Barley 
Name = Boots 
Name = Whiskers
Name = Blue Moon
Name = Daisy

Press any key to contiue.

Range例子- Enumerable.Range方法

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operators
{
  class Program
  {
     static void Main(string[] args)
     {
        // Generate a sequence of integers from 1 to 5  
        // and then select their squares.
        IEnumerable<int> squares = Enumerable.Range(1, 5).Select(x => x * x);

        foreach (int num in squares)
        {
           Console.WriteLine(num);
        }
        Console.ReadLine();
     }
  }
}

VB

Module Module1
  Sub Main()
     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 
        Console.ReadLine()
  End Sub
End Module

當C#或VB的上述程式碼被編譯和執行時,它產生了以下結果:

1
4
9
16
25

Repeat例子- Enumerable.Repeat(TResult)方法

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operators
{
  class Program
  {
     static void Main(string[] args)
     {
        IEnumerable<string> strings =
              Enumerable.Repeat("I like programming.", 3);

           foreach (String str in strings)
           {
              Console.WriteLine(str);
           }
           Console.ReadLine();
     }
  }
}

VB

Module Module1
  Sub Main()
     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

     Console.ReadLine()
  End Sub
End Module

當C#或VB的上述程式碼被編譯和執行時,它產生了以下結果:

I like programming.
I like programming.
I like programming.