LINQ to DB is an Object-Relational Mapper (ORM) that is capable of translating LINQ expressions into SQL. The word "expression" is important here. A Func
is not an expression but a delegate, you have to use Expression<Func<>>
in LINQ methods for LINQ to DB to be able to translate them. Otherwise the data will be pulled from the database first after which the Func
filters them in memory.
So your function should look like:
public static IEnumerable<iUser> Search(DataContext context,
Expression<Func<iUser, bool>> predicate)
{
return context.GetTable<iUser>().Where(predicate);
}
The return type depends on the what you want the caller of this function to be capable of. If you return IQueryable<iUser>
the caller will be able to extend the expression by their own expressions. That is, Search(context, somePredicate).Where(...)
will be translated into SQL as a whole. Returning IEnumerable
will apply any subsequent predicates (either as Func or as Expression) in memory.
Side note, in order to line up with common naming conventions, if iUser
is an interface (I have no idea if LINQ to DB supports interfaces) then you should rename it into IUser
, otherwise name it User
.