linq到实体很慢

时间:2011-06-17 18:02:04

标签: entity-framework linq-to-entities

我有一个连接几个表的LINQ To Entities。这是我使用的LINQ。

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();

它在LINQPAD中运行大约20秒,但是,如果我运行生成的sql语句,它将只有1秒。我想大部分时间花在从LINQ语句生成这个SQL语句,但为什么需要那么长时间?

1 个答案:

答案 0 :(得分:1)

EF在执行第一个查询时加载元数据,即使只有平均数量的表,这也可能需要一些时间。您是否第二次检查它是否运行得更快(不是在LInqpad中而是在代码中)?

学习EF: http://www.testmaster.ch/EntityFramework.test