2

What's the diference between

public static IEnumerable<TSource> Where<TSource>
        (this IEnumerable<TSource> source, Func<TSource, bool> predicate)

and

public static IQueryable<TSource> Where<TSource>
        (this IQueryable<TSource> source, 
         Expression<Func<TSource, bool>> predicate)

Both methods can accept a lambda expression in the same way.

List<string> fruits =
                new List<string> { "apple", "passionfruit", "banana", "mango", 
                                "orange", "blueberry", "grape", "strawberry" };
IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);

Why delegate function and expresion of delegate function exists? Must I take care about it?

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
jlvaquero
  • 8,571
  • 1
  • 29
  • 45

4 Answers4

5

An IEnumerable is just a sequence of C# items. The IEnumerable version of Select literally just iterates, in C#, through the items in the input IEnumerable, tests them against the delegate function, and yields those that match.

An IQueryable, however, can represent e.g. items in a SQL table. (Or many other kinds of data source, but SQL may be the most common.) The IQueryable version of Select can convert the expression to part of an SQL query (or similar), and passes the burden of deciding which items to return back to the SQL server (or similar).

You don't really have to worry about this distinction - LINQ can hide all these details away for you. If you're querying a list of C# objects, it'll use the IEnumerable version; if you're querying SQL via Linq-to-SQL, it'll use the IQueryable version.

Rawling
  • 49,248
  • 7
  • 89
  • 127
4

One works on IEnumerable<T>, the other one works on IQueryable<T>.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
2

When you have an IQueryable<T> a eventually used datasource (SqlServer or something other) is not already queried and the generated sql could still be modified by additional conditions.

When you habe an IEnumerable<T> the possible query is materialized and you will only filter the collection.

Viper
  • 2,216
  • 1
  • 21
  • 41
2

O.R. Mapper's answer is most adequate and simple. I think what you need to check is what's the difference between IEnumerable and IQueryable which you can find out here.

Community
  • 1
  • 1
this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137