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”.
|
var colors = new List<string>() { “red”, “green”, “blue”, “black”, “white” };
|
Lambda expression:
|
var items = colors.Where(x => x.StartsWith(“b”));
|
Query expression:
|
var items = from c in colors
where c.StartsWith(“b”)
select c;
|
Print to screen:
|
foreach (var item in items)
Console.WriteLine(item);
|
Result:
2. Collection of strings – multiple conditions
Query collection to get items with length more than 3 characters and not start with “w”.
|
var colors = new List<string>() { “red”, “green”, “blue”, “black”, “white” };
|
Lambda expression:
|
var items = colors.Where(x => x.Length > 3 && !x.StartsWith(“w”));
|
Query expression:
|
var items = from c in colors
where c.Length > 3 && !c.StartsWith(“w”)
select c;
|
Result:
3. Collection of objects – single condition
Company class for example purposes:
|
public class Company
{
public string Name { get; set; }
public int NumberOfEmployees { get; set; }
}
|
Query collection to get companies with more than 5 employees.
|
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:
|
var items = companies.Where(x => x.NumberOfEmployees > 5);
|
Query expression:
|
var items = from c in companies
where c.NumberOfEmployees > 5
select c;
|
Result:
|
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.
|
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:
|
var items = companies.Where(x => x != null && x.NumberOfEmployees > 5);
|
Query expression:
|
var items = from c in companies
where c != null && c.NumberOfEmployees > 5
select c;
|
Result:
|
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.
|
var numbers = new List<int>() { 5, 10, 15, 20, 25 };
|
Lambda expression:
|
var items = numbers.Where(x => x > 5).Where(x => x < 25);
|
Query expression:
|
var items = from n in numbers
where n > 5
where n < 25
select n;
|
Result:
Need Help with Your C# Projects?
We offer expert support and development services for projects of any size. Contact us for a free consultation and see how we can help you succeed.
CONTACT US NOW