Linq-filtering-operators

提供:Dev Guides
移動先:案内検索

LINQでの演算子のフィルタリング

フィルタリングは、特定の条件を満たす選択された要素のみを持つように結果セットを制限する操作です。

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Where Filter values based on a predicate function where Where
OfType Filter values based on their ability to be as a specified type Not Applicable Not Applicable

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