Jason Hyland
Jason Hyland

Reputation: 766

LINQ to EF - Why is this query pulling back all the data THEN counting?

Using EF 3.5 - why does the first query appear to generate a SELECT COUNT(*)... whilst the second appears to pull back all the data before performing the where?

        var model = new SageEntities();

        Func<nltranm, bool> marked_as_extracted =
            n => n.history_ref != null;

        // SELECT COUNT(*) ?
        var records_marked_as_extracted_quick = model.nltranm.Where(n => n.history_ref != null).Count();

        // Pull back all data and the count ...
        var records_marked_as_extracted_slow = model.nltranm.Where(marked_as_extracted).Count();

Upvotes: 4

Views: 189

Answers (1)

Sander Rijken
Sander Rijken

Reputation: 21615

To fix this, you should change your Func<T1, TResult> to an Expression<Func<T1, TResult>>. See this other question for explanation why

Upvotes: 6

Related Questions