2015-12-14 82 views
1
var fpslist = db.FPSinformations.Where(x => x.Godown_Code != null && x.Godown_Code == godownid).ToList(); 
     var data1 = fpslist.GroupBy(x => x.Ration_Card_Type1) 
      .Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count1) 
      }).ToList(); 
     var data2 = fpslist.GroupBy(x => x.Ration_Card_Type2) 
      .Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count2) 
      }).ToList(); 
     var data3 = fpslist.GroupBy(x => x.Ration_Card_Type3) 
      .Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count3) 
      }).ToList(); 
     var data4 = fpslist.GroupBy(x => x.Ration_Card_Type4) 
      .Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count4) 
      }).ToList(); 
     var data5 = fpslist.GroupBy(x => x.Ration_Card_Type5) 
      .Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count5) 
      }).ToList(); 

     var GodownRCCount = data1.Where(x => x.CardType_Name != null).ToList(); 
     var GodownRCCounts = GodownRCCount; 
     GodownRCCount = data2.Where(x => x.CardType_Name != null).ToList(); 
     GodownRCCounts.AddRange(GodownRCCount); 
     GodownRCCount = data3.Where(x => x.CardType_Name != null).ToList(); 
     GodownRCCounts.AddRange(GodownRCCount); 
     GodownRCCount = data4.Where(x => x.CardType_Name != null).ToList(); 
     GodownRCCounts.AddRange(GodownRCCount); 
     GodownRCCount = data5.Where(x => x.CardType_Name != null).ToList(); 
     GodownRCCounts.AddRange(GodownRCCount); 

我有10列在我的數據庫就像的LINQ集團通過與點心查詢多個列

Ration_Card_Type1 
Ration_card_count1 
Ration_Card_Type2 
Ration_card_count2 
Ration_Card_Type3 
Ration_card_count3 
Ration_Card_Type4 
Ration_card_count4 
Ration_Card_Type5 
Ration_card_count5 

現在,我要的是從它的類型得到Ration_Card_Counts的總和及其類型

預期輸出:

CardType_Name 
CardType_Count 

那麼上面的代碼工作正常,但我想以最大可能的方式對其進行優化,因爲這將位於Loop中,並且有大約150萬條記錄。

感謝

+0

您可以嘗試聯盟,但可能是你需要的,如果你關心性能編寫SQL查詢 – Thomas

+0

感謝@Thomas的答覆是他們的任何適當的建議進行修改,如果可能的 –

+0

你可以寫一個存儲過程,並把它稱爲數據庫從你的代碼中,你用什麼來連接到你的數據庫,實體框架? – Thomas

回答

1

可以使用SQL重寫相同的查詢,並把一些指標(性能):

SELECT Ration_Card_Type = Ration_Card_Type1, Ration_Card_Count = sum(Ration_card_count1) 
FROM 
    FPSinformations 
GROUP BY 
    Ration_Card_Type1 
UNION 
SELECT Ration_Card_Type = Ration_Card_Type2, Ration_Card_Count = sum(Ration_card_count2) 
FROM 
    FPSinformations 
GROUP BY 
    Ration_Card_Type2 
UNION 
SELECT Ration_Card_Type = Ration_Card_Type3, Ration_Card_Count = sum(Ration_card_count3) 
FROM 
    FPSinformations 
GROUP BY 
    Ration_Card_Type3 
UNION 
SELECT Ration_Card_Type = Ration_Card_Type4, Ration_Card_Count = sum(Ration_card_count4) 
FROM 
    FPSinformations 
GROUP BY 
    Ration_Card_Type4 
UNION 
SELECT Ration_Card_Type = Ration_Card_Type5, Ration_Card_Count = sum(Ration_card_count5) 
FROM 
    FPSinformations 
GROUP BY 
    Ration_Card_Type5 

我不知道,但此查詢讓我想想UNPIVOT也許你可以調查在這方面也是如此。

1

聯盟應該比AddRange運行得更快! 你可以嘗試以下方法:

var data = (from g in fpslist.GroupBy(x => x.Ration_Card_Type1).Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count1) 
      }).Union(
       fpslist.GroupBy(x => x.Ration_Card_Type2).Select(x => new 
       { 
        CardType_Name = x.Key, 
        CardType_Count = x.Sum(y => y.Ration_Card_Count2) 
       })).Union(
       fpslist.GroupBy(x => x.Ration_Card_Type3).Select(x => new 
       { 
        CardType_Name = x.Key, 
        CardType_Count = x.Sum(y => y.Ration_Card_Count3) 
       })).Union(
      fpslist.GroupBy(x => x.Ration_Card_Type4).Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count4) 
      })).Union(
      fpslist.GroupBy(x => x.Ration_Card_Type5).Select(x => new 
      { 
       CardType_Name = x.Key, 
       CardType_Count = x.Sum(y => y.Ration_Card_Count5) 
      })) 
         select g).ToList(); 

一般從分組我與托馬斯去! 數據庫分組對我而言要好得多,因爲您正在獲取所需的聚合數據,因此通過網絡傳輸的數據要少得多!

+0

我想你的意思是'Concat'而不是'Union' –

+1

是的!在這種情況下,Concat會比Union更快,因爲您知道列表包含唯一值。正如你在SQL中執行「UNION ALL」一樣,如果這是從SQL完成的 –