使用Dynamic Linq动态分组多列聚合

时间:2016-03-25 06:41:11

标签: linq

using System;
  using System.Collections.Generic;
 using System.Linq;
 using System.Text;


public class Program
{

  static void Main(string[] args)
  {
    // some sample data   

    List<AggregateGrouping> lstgrpelements = new List<AggregateGrouping>();
    lstgrpelements.Add(new AggregateGrouping{ Name = "Mike", CITY= "Tallahassee", STATE= "FL", value=-3.12M });
    lstgrpelements.Add(new AggregateGrouping{ Name = "Mike", CITY= "Tallahassee", STATE= "FL", value=6.57M });
    lstgrpelements.Add(new AggregateGrouping{ Name = "Steve", CITY= "Tallahassee", STATE= "FL", value=-7.34M });
    lstgrpelements.Add(new AggregateGrouping{ Name = "Steve", CITY= "Tallahassee", STATE= "FL", value=54.64M });
    lstgrpelements.Add(new AggregateGrouping{ Name = "Steve", CITY= "Orlando", STATE= "FL", value=-10.93M });
    lstgrpelements.Add(new AggregateGrouping{ Name = "Steve", CITY= "Orlando", STATE= "FL", value=235.09M });
    lstgrpelements.Add(new AggregateGrouping{ Name = "Mike", CITY= "Orlando", STATE= "NY", value=429.34M });
    lstgrpelements.Add(new AggregateGrouping{ Name = "Mike", CITY= "Orlando", STATE= "NY", value=-67.12M });

// Converts to list to iEnumrable.
IEnumerable<AggregateGrouping> enumgrpelements = lstgrpelements;
 }
}



Public Class AggregateGrouping
{
 public string Name { get; set; }
 public string CITY{ get; set; }
 public string STATE{ get; set; }
 public decimal value{ get; set; }  

}

如何使用Dynamic Linq将IEnumerable转换为groupby(CITY AND STATE)动态数据聚合(在Values上)。 CITY和STATE列是示例,但可以在运行时对多个列进行动态分组。我希望输出为值字段上的聚合。

谢谢,Ashutosh。

1 个答案:

答案 0 :(得分:0)

解决方案是使用我的产品AdaptiveLINQ。通过指定维度和度量表达式来定义多维数据集,并结合使用QueryByCubeSelect来计算聚合值。