Các toán tử lọc dùng để trả về tập kết quả chứa các phần tử thoả mãn điều kiện cụ thể nào đó. Các toán tử lọc bao gồm:

Toán tử Mô tả Cú pháp C# Cú pháp VB
Where Lọc giá trị dựa vào một hàm vị từ Where Where
OfType Lọc giá trị dựa theo kiểu cụ thể Không hỗ trợ Không hỗ trợ

 

Ví dụ chọn các chuỗi có chiều dài nhỏ hơn hay bằng 5 từ mảng words và hiển thị ra màn hình:

C#


// 1. Nguồn dữ liệu: mảng words chứa các chuỗi

string[] words = {"hello", "wonderful", "LINQ", "beautiful", "world"};

// 2. Tạo truy vấn

var shortWords = 

     from word in words

     where word.Length <= 5

     select word;

// 3. Thực thi truy vấn

foreach (var word in shortWords)

{

  Console.WriteLine(word);

}

VB


' 1. Nguồn dữ liệu: mảng words chứa các chuỗi

Dim words As String() = {"hello", "wonderful", "LINQ", "beautiful", "world"}

' 2. Tạo truy vấn

Dim shortWords = 

   From word In words

   Where word.Length <= 5

   Select word

' 3. Thực thi truy vấn

For Each word In shortWords

   Console.WriteLine(word)

Next

< Các toán tử truy vấn