Linq Group By&多列的总和查询

时间:2015-12-14 11:23:36

标签: c# performance linq visual-studio group-by

$_GET

我的数据库中有10个列,如

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);

现在我想要的是从其类型中获取Ration_Card_Counts及其类型的总和

预期输出:

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

以上代码运行正常,但我想以最大可能的方式对其进行优化,因为这将在一个循环内,并且有大约150万条记录。

由于

2 个答案:

答案 0 :(得分: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 :(得分:1)

Union应该比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();

一般来说,分组我会和托马斯一起去! 数据库分组对我来说要好得多,因为你需要聚合所需的数据,因此通过网络传输的数据要少得多!