Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: differences between single Where with multiple conditions and consecutive Wheres with single condition [duplicate]

Tags:

c#

linq

Is there any disadvantage in concatenating multiple Where in LINQ instead of using a single Where with multiple conditions?

I'm asking because using multiple Where can help to reduce complexity and improve maintainability of my code considerably.

Consider following code, chargeList is a List<Charge> which is the source of a BindingSource:

IEnumerable<Charge> matchingCharges = chargeList;
if(!string.IsNullOrWhiteSpace(channelAbbr))
    matchingCharges = matchingCharges
        .Where(c => c.ChannelAbbreviation == channelAbbr);
if(deliveryNoteDate.HasValue)
    matchingCharges = matchingCharges
        .Where(c => c.ArrivalAt == deliveryNoteDate.Value);
if(chargeID.HasValue)
    matchingCharges = matchingCharges
        .Where(c => c.ChargeID == chargeID.Value);

This concise code will handle all combinations of filter, none,one,two,all.

Otherwise i'd have to use if-else and multiple conditions in a single Where.

This is the best that comes to my mind:

// important to keep code readable:
bool filterChannel = !string.IsNullOrWhiteSpace(channelAbbr);
bool filterDate = deliveryNoteDate.HasValue;
bool filterID = chargeID.HasValue;

if(!filterChannel && !filterDate && !filterID)
{
    // take all 
    matchingCharges = chargeList;
}
else
{
    matchingCharges = chargeList
        .Where(c => 
            filterChannel ? c.ChannelAbbreviation == channelAbbr : true
            && filterDate ? c.ArrivalAt == deliveryNoteDate.Value : true
            && filterID   ? c.ChargeID ==  chargeID.Value : true);
}

So what are the differences between both, are they negligible? Does the LINQ provider matter?

like image 784
Tim Schmelter Avatar asked May 15 '14 09:05

Tim Schmelter


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 do you write two conditions in Linq?

you can use && and write all conditions in to the same where clause, or you can . Where().

Is Linq slower than for loop?

Yes, it's slower.

What is single () in C#?

Single() returns the only element from a collection, or the only element that satisfies the specified condition. If a given collection includes no elements or more than one elements then Single() throws InvalidOperationException.


1 Answers

Semantically, there is no difference in the case of Where (contrast OrderBy, which needs more care). At the implementation level, it is simply multiple predicates with simple expression trees instead of a single predicate with a complex expression tree; but most engines will cope fine with either.

For what you are doing, multiple Where is ideal.

like image 56
Marc Gravell Avatar answered Sep 21 '22 18:09

Marc Gravell