LINQ过滤运算符

过滤运算符

过滤是一种操作,以限制结果设定为使得它仅选定元素满足特定的条件。

运算符 描述 C# 查询表达式语法 VB 查询表达式语法
Where 基于谓词函数过滤值 where Where
OfType 基于过滤器值作为一个特定类型 不适用 不适用

查询表达式 Where - 示例

C#

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

namespace Operators
{
  class Program
  {
     static void Main(string[] args)
     {
        string[] words = { "humpty", "dumpty", "set", "on", "a", "wall" };

        IEnumerable<string> query = from word in words
                                    where word.Length == 3
                                    select word;
        foreach (string str in query)
           Console.WriteLine(str);
           Console.ReadLine();            
     }
  }
}

VB

Module Module1

  Sub Main()
     Dim words As String() = {"humpty", "dumpty", "set", "on", "a", "wall"}

     Dim query = From word In words
                 Where word.Length = 3
                 Select word

     For Each n In query
        Console.WriteLine(n)
     Next
        Console.ReadLine()
  End Sub
End Module

当在C#或VB上面的代码被编译和执行时,它产生了以下结果:

set

上一篇: LINQ查询运算符 下一篇: LINQ Join操作