Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple WHERE Clauses with LINQ extension methods

Tags:

c#

linq

I have a LINQ query that looks like the following:

DateTime today = DateTime.UtcNow; var results = from order in context.Orders               where ((order.OrderDate <= today) && (today <= order.OrderDate))               select order; 

I am trying to learn / understand LINQ. In some cases, I need to add two additional WHERE clauses. In an effort to do this, I'm using:

if (useAdditionalClauses) {   results = results.Where(o => o.OrderStatus == OrderStatus.Open)  // Now I'm stuck. } 

As you can see, I know how to add an additional WHERE clause. But how do I add multiple? For instance, I'd like to add

WHERE o.OrderStatus == OrderStatus.Open AND o.CustomerID == customerID

to my previous query. How do I do this using extension methods?

Thank you!

like image 495
user609886 Avatar asked Jan 09 '12 16:01

user609886


People also ask

Can we use multiple where clause in LINQ query?

Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.

How to use Where clause in LINQ?

The where clause is used in a query expression to specify which elements from the data source will be returned in the query expression. It applies a Boolean condition (predicate) to each source element (referenced by the range variable) and returns those for which the specified condition is true.

What is LINQ explain the idea of extension methods?

Linq provides standard query operators like filtering, sorting, grouping, aggregation, and concatenations, and it has many operators to achive many types of functionalities, which are called extension methods, in LINQ.

What is LINQ methods c#?

In LINQ, Method Syntax is used to call the extension methods of the Enumerable or Queryable static classes. It is also known as Method Extension Syntax or Fluent. However, the compiler always converts the query syntax in method syntax at compile time.


1 Answers

Two ways:

results = results.Where(o => (o.OrderStatus == OrderStatus.Open) &&                              (o.CustomerID == customerID)); 

or:

results = results.Where(o => (o.OrderStatus == OrderStatus.Open))                  .Where(o => (o.CustomerID == customerID)); 

I usually prefer the latter. But it's worth profiling the SQL server to check the query execution and see which one performs better for your data (if there's any difference at all).

A note about chaining the .Where() methods: You can chain together all the LINQ methods you want. Methods like .Where() don't actually execute against the database (yet). They defer execution until the actual results are calculated (such as with a .Count() or a .ToList()). So, as you chain together multiple methods (more calls to .Where(), maybe an .OrderBy() or something to that effect, etc.) they build up what's called an expression tree. This entire tree is what gets executed against the data source when the time comes to evaluate it.

like image 109
David Avatar answered Oct 07 '22 19:10

David