【问题标题】:linq to Entities very slowlinq到实体非常慢
【发布时间】:2011-06-17 18:02:04
【问题描述】:

我有一个连接几个表的 LINQ To 实体。这是我使用的 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 语句上,但是为什么要花那么长时间呢?

【问题讨论】:

    标签: entity-framework linq-to-entities


    【解决方案1】:

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

    学习英孚: http://www.testmaster.ch/EntityFramework.test

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      • 2014-12-31
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      相关资源
      最近更新 更多