I am new to LINQ and discovered yesterday that you can have multiple where clauses such as:
var items = from object in objectList
where object.value1 < 100
where object.value2 > 10
select object;
Or you can write:
var items = from object in objectList
where object.value1 < 100
&& object.value2 > 10
select object;
What is the difference between the two?
Select will always return the same number of elements in the list (regardless of a filter condition you may have). Where can return less elements depending on your filter condition.
LINQ All vs Any Operator 1. All operator is used to check whether all elements in a sequence satisfy given condition or not. 1. Any operator is used to check whether any single element in a sequence satisfy a given condition or not.
Entity Framework is an object-relational mapping (ORM) framework for connecting C# code to external databases, usually SQL Server. LINQ is a query language embedded into C# and a set of extension methods in order to make it useful.
LINQ is best suited for minimally invasive projects as it compiles each time it executes, and offers a compact, expressive, and lucid syntax for manipulating data. Stored procedure is a set of “Structured Query Language” (SQL). SQL statements are given assigned names that are stored in the database in compiled form.
The first one will be translated into:
objectList.Where(o => o.value1 < 100).Where(o=> o.value2 > 10)
while the second one will be translated in:
objectList.Where(o => o.value1 < 100 && o.value2 > 10)
So, in the first one, you will have a first filtered sequence that is filtered again (first sequence contains all the objects with value < 100, the second one containing all the objects with value > 10 from the first sequence), in while the second one you will do the same comparisons in the same labda expression. This is valid fro Linq to objects, for other providers it depends how the expression is translated.
The marked answer gets it a little bit inaccurate.
As @Philippe said, the first one will be translated into:
objectList.Where(o => o.value1 < 100).Where(o=> o.value2 > 10)
while the second one will be translated in:
objectList.Where(o => o.value1 < 100 && o.value2 > 10)
But Linq
has a little optimization for chained Where
calls.
If you inspect Linq's
source code you will see the following:
class WhereEnumerableIterator<TSource> : Iterator<TSource>
{
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate)
{
return new WhereEnumerableIterator<TSource>(source,
CombinePredicates(this.predicate, predicate));
}
}
What CombinePredicates
does is is combining the two predicates with &&
between them:
static Func<TSource, bool> CombinePredicates<TSource>(Func<TSource, bool> predicate1,
Func<TSource, bool> predicate2)
{
return x => predicate1(x) && predicate2(x);
}
So objectList.Where(X).Where(Y)
is equivalent to objectList.Where(X && Y)
except for the creation time of the query (Which is extremely short anyway) and the invocation of two predicates.
Bottom line is that it does not filter or iterate the collection two times - but one composite time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With