I just spent some time to find how to return an IQueryable from a method... and I'm still wondering if it is the right way to do it.
Here is my repository class:
public class CarRepository : ICarRepository
{
// Fake entities
private IList<Car> _entities = new List<Car>()
{
new Car() { Brand = "Lamborghini", Name = "Huracán"},
new Car() { Brand = "BMW", Name = "X6" }
};
// Allows deferred execution/further refinement
public IQueryable<Car> FindByQueryable(Expression<Func<Car, bool>> predicate)
{
var query = _entities.Where(predicate.Compile()).AsQueryable();
return query;
}
// Returning an IList or IEnumerable
public IList<Car> FindBy(Expression<Func<Car, bool>> predicate)
{
return _entities.Where(predicate.Compile()).ToList();
}
}
At first I thought that something like that should work, but it was not compiling:
public IQueryable<Car> FindByQueryable(Expression<Func<Car, bool>> predicate)
{
var query = _entities.Where(predicate);
return query;
}
Am I right with the predicate.Compile() and .AsQueryable?
Thanks for you help! Bastien