如何在字典列表上动态构建group-by [英] How to build group-by dynamically on a list of dictionaries

查看:176
本文介绍了如何在字典列表上动态构建group-by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在IEnumerable上执行一个groupby。问题是编译时我不知道我想要分组的字段。我已经在堆栈上找到了另一篇文章,解释了当类被知道时如何做到这一点并且具有属性,但在我的情况下,我正在处理一个字典,键也只在运行时知道。



我的代码将类似于这样的东西知道这不编译...):

 私有对象GetValuesGroupedBy(List< string> groupbyNames,List< string> summableNames )
{
//获取网格中的项目列表
var listOfDicos = grid.AllItems;

return listOfDicos
.GroupBy(x => new {x [groupbyNames [0]],
x [groupbyNames [1]],
x [groupbyNames [2 ]]})
.Select(group => new {group.Key,
group.Sum(x => x [summableNames [0]]),
group.Sum x => x [summableNames [1]])});
}

任何想法?我已经开始研究动态LINQ,但是被卡住了(因为我没有使用属性,而是使用Key / Value集合)...



感谢大家!!



Sean

解决方案

所以我可以让groupby工作(select语句是另一个问题)。感谢c0d1ng把我放在正确的道路上。语法不是那么简单,因为我使用的是索引器而不是属性...



以下是我的代码:

  private void GetValuesGroupedBy(List< Dictionary< string,object>> list,List< string> groupbyNames,List< string> summableNames)
{
//构建groupby字符串
StringBuilder groupBySB = new StringBuilder();
groupBySB.Append(new();
bool useComma = false;
foreach(groupbyNames中的var名称)
{
if(useComma)
groupBySB.Append(,);
else
useComma = true;

groupBySB.Append(it [\);
groupBySB。 append(name);
groupBySB.Append(\]);
groupBySB.Append(as);
groupBySB.Append(name);
}
groupBySB.Append());

var groupby = list.GroupBy(groupBySB.ToString(),it);
}


I am trying to perform a groupby on an IEnumerable. The problem is that I do not know at compile-time which fields i want to groupby. I have found another post on stack that explains how to do this when the class is known and has properties, but in my case i am dealing with a dictionary and the keys are also only known at run-time.

My code would resemble something like this (i know this doesn't compile...):

private object GetValuesGroupedBy(List<string> groupbyNames, List<string> summableNames)
{
     // get the list of items in the grid
     var listOfDicos = grid.AllItems;

     return listOfDicos
                .GroupBy(x => new { x[groupbyNames[0]], 
                                    x[groupbyNames[1]], 
                                    x[groupbyNames[2]] })
                .Select(group => new { group.Key, 
                                       group.Sum(x => x[summableNames[0]]), 
                                       group.Sum(x => x[summableNames[1]]) });
}  

Any ideas? I've started looking into Dynamic LINQ but am stuck (because I am not using properties but a Key/Value collection)...

Thanks everybody!!

Sean

解决方案

So I am able to get the groupby to work... (the select statement is another question). Thanks to c0d1ng for putting me on the right path. The syntax was not so trivial because I am using indexers and not properties...

Below is my code:

    private void GetValuesGroupedBy(List<Dictionary<string, object>> list, List<string> groupbyNames, List<string> summableNames)
    {
        // build the groupby string
        StringBuilder groupBySB = new StringBuilder();
        groupBySB.Append("new ( ");
        bool useComma = false;
        foreach (var name in groupbyNames)
        {
            if (useComma)
                groupBySB.Append(", ");
            else
                useComma = true;

            groupBySB.Append("it[\"");
            groupBySB.Append(name);
            groupBySB.Append("\"]");
            groupBySB.Append(" as ");
            groupBySB.Append(name);
        }
        groupBySB.Append(" )");

        var groupby = list.GroupBy(groupBySB.ToString(), "it");
    }

这篇关于如何在字典列表上动态构建group-by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆