2012-06-03 78 views
1

下面是我正在使用的代碼和它從中拉取的數據庫表中有大約92000條記錄。它現在拉的方式是拉動所有92000條記錄然後進行過濾。 我期望做的是過濾從數據庫的初始拉,以便它不需要大約40秒來加載頁面。 這事我還是新的,所以我失去了對如何做到這一點,並使其與我的觀點工作加速大Linq到實體調用MVC3

public ViewResult Makes() 
{ 
    var items = (from item in DBCacheHelper.recallslist 
       orderby item.MFGTXT ascending 
       select item.ToDomainRecall()).GroupBy(item => item.MFGTXT).Select(grp => grp.First()); 


    return View(items); 
} 

public static IEnumerable<Recall> recallslist 
{ 
    get 
    { 
     if (c["GetAllRecalls"] == null) 
     { 
      c.Insert("GetAllRecalls", GetAllRecalls()); 
      return (IEnumerable<Recall>)c["GetAllRecalls"]; 
     } 
     else 
     { 
      return (IEnumerable<Recall>)c["GetAllRecalls"]; 
     } 
    } 
} 



public static IEnumerable<Recall> GetAllRecalls() 
{ 
    using (DealerContext context = new DealerContext()) 
    { 
     var items = from item in context.recalls.ToList<Recall>() 
        select item.ToDomainRecall(); 
     return items.ToList<Recall>(); 
    } 
} 


SELECT 
[Extent1].[RecallsId] AS [RecallsId], 
[Extent1].[RECORD_ID] AS [RECORD_ID], 
[Extent1].[CAMPNO] AS [CAMPNO], 
[Extent1].[MAKETXT] AS [MAKETXT], 
[Extent1].[MODELTXT] AS [MODELTXT], 
[Extent1].[YEARTXT] AS [YEARTXT], 
[Extent1].[MFGCAMPNO] AS [MFGCAMPNO], 
[Extent1].[COMPNAME] AS [COMPNAME], 
[Extent1].[MFGNAME] AS [MFGNAME], 
[Extent1].[BGMAN] AS [BGMAN], 
[Extent1].[ENDMAN] AS [ENDMAN], 
[Extent1].[RCLTYPECD] AS [RCLTYPECD], 
[Extent1].[POTAFF] AS [POTAFF], 
[Extent1].[ODATE] AS [ODATE], 
[Extent1].[INFLUENCED_BY] AS [INFLUENCED_BY], 
[Extent1].[MFGTXT] AS [MFGTXT], 
[Extent1].[RCDATE] AS [RCDATE], 
[Extent1].[DATEA] AS [DATEA], 
[Extent1].[RPNO] AS [RPNO], 
[Extent1].[FMVSS] AS [FMVSS], 
[Extent1].[DESC_DEFECT] AS [DESC_DEFECT], 
[Extent1].[CONEQUENCE_DEFECT] AS [CONEQUENCE_DEFECT], 
[Extent1].[CORRECTIVE_ACTION] AS [CORRECTIVE_ACTION], 
[Extent1].[NOTES] AS [NOTES], 
[Extent1].[RCL_CMPT_ID] AS [RCL_CMPT_ID] 
FROM [dbo].[Recalls] AS [Extent1] 

更新:

最後,我想只從召回拉記錄表,其中MFGTXT等於 MakeName在AUTOMAKE表

public class AutoMake 
{ 
    [Key] 
    public int MakeID { get; set; } 
    public string MakeName { get; set; } 

public AutoMake ToDomainAutoMakes() 
{ 
    return new AutoMake 
    { 
     MakeID = this.MakeID, 
     MakeName = this.MakeName 
    }; 
} 

}

public class Recall 
{ 
    [Key] 
    public int RecallsId { get; set; } 
    public string RECORD_ID { get; set; } 
    public string CAMPNO { get; set; } 
    public string MAKETXT { get; set; } 
    public string MODELTXT { get; set; } 
    public string YEARTXT { get; set; } 
    public string MFGCAMPNO { get; set; } 
    public string COMPNAME { get; set; } 
    public string MFGNAME { get; set; } 
    public string BGMAN { get; set; } 
    public string ENDMAN { get; set; } 
    public string RCLTYPECD { get; set; } 
    public string POTAFF { get; set; } 
    public string ODATE { get; set; } 
    public string INFLUENCED_BY { get; set; } 
    public string MFGTXT { get; set; } 
    public string RCDATE { get; set; } 
    public string DATEA { get; set; } 
    public string RPNO { get; set; } 
    public string FMVSS { get; set; } 
    public string DESC_DEFECT { get; set; } 
    public string CONEQUENCE_DEFECT { get; set; } 
    public string CORRECTIVE_ACTION { get; set; } 
    public string NOTES { get; set; } 
    public string RCL_CMPT_ID { get; set; } 

    public Recall ToDomainRecall() 
    { 
     return new Recall 
     { 
      RECORD_ID = this.RECORD_ID, 
      CAMPNO = this.CAMPNO, 
      MAKETXT = this.MAKETXT, 
      MODELTXT = this.MODELTXT, 
      YEARTXT = this.YEARTXT, 
      MFGCAMPNO = this.MFGCAMPNO, 
      COMPNAME = this.COMPNAME, 
      MFGNAME = this.MFGNAME, 
      BGMAN = this.BGMAN, 
      ENDMAN = this.ENDMAN, 
      RCLTYPECD = this.RCLTYPECD, 
      POTAFF = this.POTAFF, 
      ODATE = this.ODATE, 
      INFLUENCED_BY = this.INFLUENCED_BY, 
      MFGTXT = this.MFGTXT, 
      RCDATE = this.RCDATE, 
      DATEA = this.DATEA, 
      RPNO = this.RPNO, 
      FMVSS = this.FMVSS, 
      DESC_DEFECT = this.DESC_DEFECT, 
      CONEQUENCE_DEFECT = this.CONEQUENCE_DEFECT, 
      CORRECTIVE_ACTION = this.CORRECTIVE_ACTION, 
      NOTES = this.NOTES, 
      RCL_CMPT_ID = this.RCL_CMPT_ID 

     }; 
    } 
} 
+0

您能否顯示如何過濾記錄? – Eranga

+0

我認爲你需要一個外鍵... – RPM1984

回答

0

如果您要添加的存儲庫方法之外的服務器端過濾,你需要回到你的類型爲IQueryable的,而不是IEnumerable和不叫.ToList,.AsEnumerable,或會導致任何其他方法。 GetEnumerator被調用。另外,你的cast {(IEnumerable)c [「GetAllRecalls」];'強制LINQ to Objects用於後續請求,而不是保留表達式樹並使用Entity Framework。也就是說,您可能需要將您的調用移至ToDomainRecall方法,以便在應用了附加過濾器之後,因爲該方法不能轉換爲您的數據庫。以下是您需要進行的一些更改:

public ViewResult Makes() 
{ 
    var items = (from item in DBCacheHelper.recallslist 
       orderby item.MFGTXT ascending 
       select item.ToDomainRecall()).GroupBy(item => item.MFGTXT).Select(grp => grp.First()); 


    return View(items); 
} 

public static IQueryable<Recall> recallslist 
{ 
    get 
    { 
     if (c["GetAllRecalls"] == null) 
     { 
      c.Insert("GetAllRecalls", GetAllRecalls(context)); 
     } 
     return c["GetAllRecalls"]; 

    } 
} 



public static IQueryable<Recall> GetAllRecalls(DealerContext context) 
{ 
     var items = context.recalls; 
     return items; 
} 
0

看起來你的DBaccess是在DBCacheHelper.recallslist的調用中完成的。

您需要編輯從該函數中運行的SQL。如Eranga指出的那樣,你不會顯示如何過濾大量的記錄到更少的記錄。我假設你一次需要20或100個?如果是的話,請看這裏接受的答案:

efficient way to implement paging

具體來說,這一部分它展示瞭如何只檢索行X到Y(其中x = @ P0 + 1和Y = @ P0 + @ P1) :

SELECT [t1].[CodCity], 
    [t1].[CodCountry], 
    [t1].[CodRegion], 
    [t1].[Name], 
    [t1].[Code] 
FROM (
    SELECT ROW_NUMBER() OVER (
     ORDER BY [t0].[CodCity], 
     [t0].[CodCountry], 
     [t0].[CodRegion], 
     [t0].[Name], 
     [t0].[Code]) AS [ROW_NUMBER], 
     [t0].[CodCity], 
     [t0].[CodCountry], 
     [t0].[CodRegion], 
     [t0].[Name], 
     [t0].[Code] 
    FROM [dbo].[MtCity] AS [t0] 
    ) AS [t1] 
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1 
ORDER BY [t1].[ROW_NUMBER]