Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将聚合动态应用于Linq IGROUP元素_C#_Linq - Fatal编程技术网

C# 将聚合动态应用于Linq IGROUP元素

C# 将聚合动态应用于Linq IGROUP元素,c#,linq,C#,Linq,我需要在集合上做一些基本的聚合。要分组和聚合的字段每次都会更改,要应用的聚合函数也会更改。我能够对元素进行分组,现在我想知道如何应用用户选择的自定义聚合函数 例如: // Data I want to aggregate Collection<Dictionary<string,object>> userData; // The key is the field, and the value is the name of the aggregate function to

我需要在
集合上做一些基本的聚合
。要分组和聚合的字段每次都会更改,要应用的聚合函数也会更改。我能够对元素进行分组,现在我想知道如何应用用户选择的自定义聚合函数

例如:

// Data I want to aggregate
Collection<Dictionary<string,object>> userData;
// The key is the field, and the value is the name of the aggregate function to apply. Ex: {"distance", "average"},{"time","sum"},{"price","customAgg"}
Dictionary<string,string> fieldsToAggregate; 

var groupedData = userData.GroupBy( /* here I selected all the fields that are not being aggregated */ )

foreach(var group in groupedData)
{
  // Here is where I don't know how to best handle it ...
}

这太静态了,其他应用程序/类将无法扩展此功能。有更好的方法吗?可能使用
.Aggregate()
与委托一起使用?

是的,您可以使用Aggregate与委托一起使用,您尝试过吗?@ArsenMkrtchyan我开始了这条路线,看起来很有希望。我只是想看看是否有一种更灵活、更易于维护的方法,这取决于这个字符串文本存储在哪里,以及是否可以直接保存操作。。。如果可以的话,我会和代表们一起去,如果可以的话。动态Linq有帮助吗?看起来您正在尝试构建透视表。您看过.net中数百种透视表的实现吗?
case "sum":
   group.Sum(/* I select here the appropriate field */);
   break;