2014-01-08 34 views
1

第一次嘗試預編譯linq查詢,看起來像我做錯了什麼。如何使用預編譯的Linq查詢

我想在驗證器查詢中使用預編譯查詢,這將在每次使用時調用大約14k次。我想在構造函數中實例化查詢(同樣,第一次使用這些不確定這是否正確使用),所以類對象將能夠調用編譯的查詢。

這裏是Func的性質,並在構造函數中

private static Func<DataContext, ZipCodeTerritory, IQueryable<ZipCodeTerritory>> SearchEffectiveDate; 
    public ZipCodeValidatorCompiled() 
    { 
     SearchEffectiveDate = CompiledQuery.Compile((DataContext db, ZipCodeTerritory zipCode) 
     => 
     from z in db.GetTable<ZipCodeTerritory>() 
     where z.DrmTerrDesc.Equals(zipCode.DrmTerrDesc) && 
       z.IndDistrnId.Equals(zipCode.IndDistrnId) && 
       z.StateCode.Equals(zipCode.StateCode) && 
       (z.ZipCode.Equals(null) ? z.ZipCode.Equals(null) : z.ZipCode.Equals(zipCode.ZipCode)) 
     select z 
     ); 
    } 

實例化,這裏是它是如何調用的驗證方法

private static string ValidateEffectiveDate(ZipCodeTerritory zipCode) 
    { 
     using (var _db = new AgentResources()) 
     { 
      IQueryable<ZipCodeTerritory> terrList = SearchEffectiveDate(_db, zipCode); 

      foreach (var zip in terrList) 
      { 
       if (zip.EffectiveDate >= zipCode.EndDate) 
       { 
        return 
         "End Date must be greater than Effective Date of any other record sharing the same DRM Territory Description, Territory, State and Zip Code; "; 
       } 
      }     
     } 

     return null; 
    } 

我遇到的一個問題在驗證方法中使用此聲明

IQueryable<ZipCodeTerritory> terrList = SearchEffectiveDate(_db, zipCode); 

Visual St UDIO SearchEffectiveDate(_db, zipCode)是給我下面的錯誤:

  • 錯誤20代表「System.Func>」已經一些無效參數
  • 錯誤21參數1:無法從「Monet.Models.AgentResources」到「系統轉換。 Data.Linq.DataContext'

我正在使用this tutorial加速Linq查詢。

+0

FYI,查詢的第一個參數不一定必須被定義爲'DataContext',也可以是從它派生。所以你可以在你的地方使用爲你生成的數據上下文,這樣你可以在你的查詢中輸入更強的信息。 –

+0

你可以使用'DbContext'對象嗎? – NealR

+0

不是直接。 EF的'DbContext'不直接支持編譯查詢。但是你可以從它得到一個'ObjectContext',它可以在編譯查詢中使用。將其轉換爲'IObjectContextAdapter'並獲取ObjectContext來創建您的查詢。我會看到有關你的問題的答案,應該是可行的,只需做一些調整。 –

回答

0

這裏的問題是我沒有使用DataContext對象,而是我的DbContext類型的對象AgentResoources。通過在類中添加一個靜態的DataContext對象作爲參數並在構造函數中實例化它,使用標準連接字符串,就像使用ojbect那樣,我就能夠編譯代碼。

參數/構造

private static Func<DataContext, ZipCodeTerritory, IQueryable<ZipCodeTerritory>> SearchEffectiveDate; 
    private static DataContext _dbContext; 
    public ZipCodeValidatorCompiled() 
    { 
     _dbContext = new DataContext(ConfigurationManager.AppSettings.Get("db")); 
     SearchEffectiveDate = CompiledQuery.Compile((DataContext db, ZipCodeTerritory zipCode) 
     => 
     from z in db.GetTable<ZipCodeTerritory>() 
     where z.DrmTerrDesc.Equals(zipCode.DrmTerrDesc) && 
       z.IndDistrnId.Equals(zipCode.IndDistrnId) && 
       z.StateCode.Equals(zipCode.StateCode) && 
       (z.ZipCode.Equals(null) ? z.ZipCode.Equals(null) : z.ZipCode.Equals(zipCode.ZipCode)) 
     select z 
     ); 
    } 

方法

IQueryable<ZipCodeTerritory> terrList = SearchEffectiveDate(_dbContext, zipCode); 
foreach (var zip in terrList) 
{ 
    if (zip.EffectiveDate >= zipCode.EndDate) 
    { 
     return 
      "End Date must be greater than Effective Date of any other record sharing the same DRM Territory Description, Territory, State and Zip Code; "; 
    } 
}