LINQ 中的生成操作
由生成运算符创建新的值序列。
运算符 | 描述 | C# 查询表达式语法 | VB 查询表达式语法 |
---|---|---|---|
DefaultIfEmpty | 应用于空序列时,在序列内生成默认元素 | 不适用 | 不适用 |
Empty | 返回空的值序列,并且是最最简单的代数运算符 | 不适用 | 不适用 |
Range | 生成具有整数或数字序列的集合 | 不适用 | 不适用 |
Repeat | 生成包含特定长度的重复值的序列 | 不适用 | 不适用 |
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(" Press 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 continue.
Example of Range - Enumerable.Range Method
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
Example of Repeat - Enumerable.Repeat(Of TResult) Method
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.