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("\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 continue.
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(Of 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.
linq_query_operators.htm
广告