Where is a LINQ functionality to filter data in a query with given criteria.
Each of below examples is presented in C# with both Lambda and Query expression.
1. Collection of strings – single condition
Query collection to get items which start with “b”.
1 |
var colors = new List<string>() { "red", "green", "blue", "black", "white" }; |
Lambda expression:
1 |
var items = colors.Where(x => x.StartsWith("b")); |
Query expression:
1 2 3 |
var items = from c in colors where c.StartsWith("b") select c; |
Print to screen:
1 2 |
foreach (var item in items) Console.WriteLine(item); |
Result:
1 2 |
blue black |
2. Collection of strings – multiple conditions
Query collection to get items with length more than 3 characters and not start with “w”.
1 |
var colors = new List<string>() { "red", "green", "blue", "black", "white" }; |
Lambda expression:
1 |
var items = colors.Where(x => x.Length > 3 && !x.StartsWith("w")); |
Query expression:
1 2 3 |
var items = from c in colors where c.Length > 3 && !c.StartsWith("w") select c; |
Result:
1 2 3 |
green blue black |
3. Collection of objects – single condition
Company class for example purposes:
1 2 3 4 5 |
public class Company { public string Name { get; set; } public int NumberOfEmployees { get; set; } } |
Query collection to get companies with more than 5 employees.
1 2 3 4 5 6 7 8 |
var companies = new List<Company>() { new Company() { Name = "Company 1", NumberOfEmployees = 10 }, new Company() { Name = "Company 2", NumberOfEmployees = 2 }, new Company() { Name = "Company 3", NumberOfEmployees = 25 }, new Company() { Name = "Company 4", NumberOfEmployees = 5 }, new Company() { Name = "Company 5", NumberOfEmployees = 40 } }; |
Lambda expression:
1 |
var items = companies.Where(x => x.NumberOfEmployees > 5); |
Query expression:
1 2 3 |
var items = from c in companies where c.NumberOfEmployees > 5 select c; |
Result:
1 2 3 |
Company 1 Company 3 Company 5 |
4. Collection of objects – null item
Query collection to get not null elements where number of employees is more than 5.
1 2 3 4 5 6 7 8 |
var companies = new List<Company>() { new Company() { Name = "Company 1", NumberOfEmployees = 10 }, null, new Company() { Name = "Company 3", NumberOfEmployees = 25 }, new Company() { Name = "Company 4", NumberOfEmployees = 5 }, new Company() { Name = "Company 5", NumberOfEmployees = 40 } }; |
One of the list elements is null which creates a risk of NullReferenceException. It can be prevented by additional condition: sth != null.
Lambda expression:
1 |
var items = companies.Where(x => x != null && x.NumberOfEmployees > 5); |
Query expression:
1 2 3 |
var items = from c in companies where c != null && c.NumberOfEmployees > 5 select c; |
Result:
1 2 3 |
Company 1 Company 3 Company 5 |
5. Collection of numbers – multiple where
Query collection to get numbers greater than 5 and smaller than 25 using multiple where statements.
1 |
var numbers = new List<int>() { 5, 10, 15, 20, 25 }; |
Lambda expression:
1 |
var items = numbers.Where(x => x > 5).Where(x => x < 25); |
Query expression:
1 2 3 4 |
var items = from n in numbers where n > 5 where n < 25 select n; |
Result:
1 2 3 |
10 15 20 |