Why is my LINQ statement returning IEnumerable?

I have two very similar methods:

public IQueryable<User> Find(Func<User, bool> exp)
{
    return db.Users.Where(exp);
}

public IQueryable<User> All()
{
    return db.Users.Where(x => !x.deleted);
}

The top one will not compile if it returns IEnumerable, not IQueryable.

Why is this?

Also, I know that I can add "AsQueryable ()" at the end, and it will work. Who cares? Any performance hits? I understand that IQueryable has deferred execution and such, am I still getting this benefit?

+5
source share
1 answer

Enumerable.Whereaccepts Func<T, bool>.

Queryable.Whereaccepts Expression<Func<T, bool>>.

You call Where with Func<T, bool>, so only the call is applicable Enumerable.Whereand returns IEnumerable<T>.

Change your method to:

public IQueryable<User> Find(Expression<Func<User, bool>> exp)
{
    return db.Users.Where(exp);
}

. , SQL.

+17

All Articles