简体   繁体   中英

Diferences between IEnumerable<TSource> Where & IQueryable<TSource> Where extension methods

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?

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 eg 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.

一个适用于IEnumerable<T> ,另一个适用于IQueryable<T>

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.

OR 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM