Given a "standard" Enumerable.Where
they are totally equivalent (from the result standpoint). The first one will be converted to the second one by the private class WhereEnumerableIterator<TSource>
, to be exact by this method:
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate) {
return new WhereEnumerableIterator<TSource>(source, CombinePredicates(this.predicate, predicate));
}
that will combine the predicates in this way:
static Func<TSource, bool> CombinePredicates<TSource>(Func<TSource, bool> predicate1, Func<TSource, bool> predicate2) {
return x => predicate1(x) && predicate2(x);
}
See the x => predicate1(x) && predicate2(x)
?
Technically the second one will be a little faster, because the first one will have some more delegate calls to do, but unless you are filtering million of rows, the time difference is negligible.
Note that, while the Enumerable.Where
does funny tricks, even a non-smart .Where
, like:
public static class SimpleEnumerable
{
public static IEnumerable<TSource> SimpleWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (TSource element in source)
{
if (predicate(element))
{
yield return element;
}
}
}
}
would be totally equivalent (but even slower!). See the example here: https://ideone.com/QAQZ65