VBAHole
VBAHole

Reputation: 1518

Great difference in Entity Framework query speed. But why?

I am writing a method that allows users to search for records from a table based on some criteria. they may not always submit all criteria so i am trying to craft the EF query so that it can be built up in pieces based on what the user has provided as criteria. The relevant piece is here:

using (var db = new EntityModels.My_Entities())
{
    //very fast
    List<EntityModels.TBLEFFORT> effs = db.TBLEFFORT.Where(a => a.ANID == "5F180A0000K0").ToList();

    //very slow
    IQueryable<EntityModels.TBLEFFORT> query = db.TBLEFFORT.AsQueryable();
    query.Where(a => a.ANID == "5F180A0000K0");
    List<EntityModels.TBLEFFORT> efforts = query.ToList();
}

The first line runs very quickly and return 150 records or so. The second construct takes longer than i'm even willing to wait (at least 2 minutes before i gave up). Why are they so different? I want to use the second construct so i can build upon the query object based on whether a specific criteria was included by the user. What am i missing here?

Upvotes: 1

Views: 59

Answers (1)

ycsun
ycsun

Reputation: 1855

This line is incorrect:

query.Where(a => a.ANID == "5F180A0000K0");

It should be:

query = query.Where(a => a.ANID == "5F180A0000K0");

The Where method is a function that returns a new IQueryable. It doesn't modify the IQueryable against which it is called.

Upvotes: 4

Related Questions