2011-06-17 81 views
1

我有一個LINQ To Entities加入了幾個表。這裏是我使用的LINQ。linq to實體很慢

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

EF在執行第一個查詢時加載元數據,即使只有平均數量的表,這可能也需要一些時間。你有沒有檢查它是否第二次運行得更快(不是在LInqpad中,而是在代碼中)?

學習EF: http://www.testmaster.ch/EntityFramework.test