J.W.
J.W.

Reputation: 18181

linq to Entities very slow

I have a LINQ To Entities which join a couple of tables. Here is the LINQ I use.

var lastestEntry = (from c in etDataContext.Child_HomeVisitor
                                join s in etDataContext.ServiceCoordinators
                                on c.HomeVisitorID equals s.ServiceCoordinatorID
                                join ch in etDataContext.CountyHomeVisitorAgencies
                                on c.CountyHomeVisitorAgencyId equals ch.CountyHomeVisitorAgencyID
                                join a in etDataContext.Agencies
                                on ch.AgencyID equals a.AgencyID
                                join f in etDataContext.ServiceCoordinatorFundingSourceTypes
                                on c.PrimaryFundingSourceID equals f.ServiceCoordinatorFundingSourceTypeId
                                into joinFundingSource
                                from j in joinFundingSource.DefaultIfEmpty()
                                where c.ChildID.Equals(childID)
                                orderby c.EffectiveStartDate descending, c.CreatedDateTime descending
                                select new
                                           {                                                                                         
                                               c.EffectiveStartDate,                                                  
                                               s.FirstName,
                                               s.LastName,
                                               a.Description,
                                               j.FundingSource
                                           }).FirstOrDefault();

It runs about 20 seconds in LINQPAD, however, if I run the generated sql statement , it will be only 1 second. I guess most of time is spent on generating this SQL statement from LINQ statement, but why it would take that long?

Upvotes: 1

Views: 393

Answers (1)

yonexbat
yonexbat

Reputation: 3012

EF loads the metadata while executing the first query and this may take some time even with only an average number of tables. Did you check if it runs faster the second time (not in LInqpad but in code)?

Learning the EF: http://www.testmaster.ch/EntityFramework.test

Upvotes: 1

Related Questions