I'm using an ObjectSet's methods to do various queries in a table, namely, selecting a few records from it as such:
var results = Repository.Find(c => c.Enabled == 1).ToList();
Here is the Find method of my repository:
public IEnumerable<T> Find(Func<T, bool> predicate)
{
try
{
return _objectSet.Where<T>(predicate);
}
catch
{
throw;
}
}
Now, if there is about 1,000,000 records in the targeted table, I can see the process's memory usage grow quite a lot even if the Find call I'm doing should return a few records at most.
It seems all the records are pulled client side, then filtered. This is obviously not what I want LINQ to do.
Do you see anything obviously wrong with what I'm doing ?
Thanks in advance.
I think you should use Expression<Func<T, bool>>
instead of a plain Func<T, bool>
:
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate) {
// your code
}
Where
is overloaded (see ObjectSet Class), and the Func<T>
-overload is defined by IEnumerable<T>
, whereas Expression<TDelegate>
is used by IQueryable<T>
. Because predicate
is a Func<T>
the compiler invokes the extension method defined for IEnumerable<T>
, which in turn fetches all the records and does LINQ to objects.