1

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.

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
  • 1
    don't use IEnumerable it is pulling every datarow at once into memory instead you should use IQueryable – MUG4N Feb 08 '13 at 15:09
  • 1
    Your `Find` method accomplishes nothing productive at all. Why are you not just using `Where` directly on the `Repository`? – Servy Feb 08 '13 at 15:11
  • @MUG4N He's only using that for his output, which should already be filtered, so that's not the problem. – Servy Feb 08 '13 at 15:12

1 Answers1

2

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.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79