advertise with us

C# – LINQ Where Examples

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”.

Lambda expression:

Query expression:

Print to screen:

Result:

2. Collection of strings – multiple conditions

Query collection to get items with length more than 3 characters and not start with “w”.

Lambda expression:

Query expression:

Result:

3. Collection of objects – single condition

Company class for example purposes:

Query collection to get companies with more than 5 employees.

Lambda expression:

Query expression:

Result:

4. Collection of objects – null item

Query collection to get not null elements where number of employees is more than 5.

One of the list elements is null which creates a risk of NullReferenceException. It can be prevented by additional condition: sth != null.

Lambda expression:

Query expression:

Result:

5. Collection of numbers – multiple where

Query collection to get numbers greater than 5 and smaller than 25 using multiple where statements.

Lambda expression:

Query expression:

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