This post is part of a series called LINQ Examples that brings practical examples of using LINQ in C#.
Today I'll show how to use WHERE clause in LINQ. This kind of operators are called LINQ Restriction Operators. We start from the simplest use of WHERE clause in LINQ - select all numbers from an array less than 10:
int[] numbers = { 15, 42, 14, 3, 9, 83, 64, 76, 23, 0 }; var lessThanTen = from n in numbers where n < 10 select n; foreach (var x in lessThanTen) { Console.Write(x + ", "); // 3, 9, 0 }Now let's see more complicated example of using WHERE clause:
var employees = from e in employeeList where e.EmployeeID > 1000 && e.EmployeePosition == "Developer" select e; foreach (var e in employees) { Console.Write(e.EmployeeName + ", "); // John, Suzan }As you can see, LINQ syntax is quite similar to SQL. Once you get used to its syntax you will write LINQ queries faster.
No comments:
Post a Comment