2015-12-14 45 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完成的 –