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:使用字典索引器上的和进行选择_C#_Linq_Select_Dynamic Linq - Fatal编程技术网

C#动态LINQ:使用字典索引器上的和进行选择

C#动态LINQ:使用字典索引器上的和进行选择,c#,linq,select,dynamic-linq,C#,Linq,Select,Dynamic Linq,我使用动态LINQ创建groupby并动态选择。我的项是键/值集合(字典),因此它们不包含属性(这是设计要求,无法更改)。我能够在中解决groupby部分,但它在select方法中似乎不起作用 我的代码如下: private void GetValuesGroupedBy(List<Dictionary<string, object>> list, List<string> groupbyNames, List<string> summab

我使用动态LINQ创建groupby并动态选择。我的项是键/值集合(字典),因此它们不包含属性(这是设计要求,无法更改)。我能够在中解决groupby部分,但它在select方法中似乎不起作用

我的代码如下:

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

        // and now the select string
        StringBuilder selectSB = new StringBuilder();
        selectSB.Append("new ( ");
        useComma = false;
        foreach (var name in groupbyNames)
        {
            if (useComma)
                selectSB.Append(", ");
            else
                useComma = true;

            selectSB.Append("Key.")
                //.Append(name)
                //.Append(" as ")
                .Append(name);
        }
        foreach (var name in summableNames)
        {
            if (useComma)
                selectSB.Append(", ");
            else
                useComma = true;

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

        var groupby = list.GroupBy(groupBySB.ToString(), "it");
        var select = groupby.Select(selectSB.ToString());
    }
private void GetValuesGroupedBy(列表、列表groupbyNames、列表summableNames)
{
//构建groupby字符串
StringBuilder groupBySB=新建StringBuilder();
groupBySB.Append(“新(”);
bool-usecoma=false;
foreach(groupbyNames中的变量名)
{
如果(使用逗号)
groupBySB.追加(“,”);
其他的
usecoma=true;
Append(“it[\”);
groupBySB.Append(名称);
groupBySB.Append(“\”]);
groupBySB.追加(“as”);
groupBySB.Append(名称);
}
groupBySB.追加(“)”;
//现在选择字符串
StringBuilder selectSB=新建StringBuilder();
选择某人附加(“新建”);
usecoma=false;
foreach(groupbyNames中的变量名)
{
如果(使用逗号)
选择某人。附加(“,”);
其他的
usecoma=true;
选择某人追加(“键”)
//.Append(名称)
//.Append(“as”)
.附加(姓名);
}
foreach(summableNames中的变量名称)
{
如果(使用逗号)
选择某人。附加(“,”);
其他的
usecoma=true;
选择sb.Append(“总和(”)
.Append(“它[\”“)
.Append(名称)
.Append(“\”]”)
.附加(“)为”)
.附加(姓名);
}
选择某人。附加(“)”;
var groupby=list.groupby(groupBySB.ToString(),“it”);
var select=groupby.select(selectSB.ToString());
}
select字符串的关键部分正常,但总和部分不起作用。假设我想要的键名为value,我尝试了:

  • “总和(值)”:应为ParseException:表达式

  • “Sum(\“value\”):ParseException:应为表达式

  • “Sum(it[\“value\”)”:语法异常:不存在适用的聚合方法“Sum”

  • “Sum(it[value])”:ParseException:类型“Dictionary”中不存在属性或字段“value”

  • “总和([\“值\”])”:语法异常:应为表达式

但都失败了。有什么想法吗

谢谢!
Sean

您的语法正确(我的测试字段是“一”和“二”)

但是,没有linq方法Sum(对象),您需要告诉它求和的内容(int、float、decimal等)


最简单的方法是把字典的定义改成字典我自己也面临这个问题;问题是
Sum()
将列视为类型
对象
(可应用于
Convert()
,或
Max()
偶数,但不能
Sum()
),因此失败;请注意,它表示没有适用的聚合函数

解决方案是使用到整数的内联转换。Dynamic LINQ支持此转换,在您的示例中可以按如下方式进行转换:

selectSB.Append("Sum(")
    .Append("Convert.ToInt32(") // Add this...
    .Append("it[\"")
    .Append(name)
    .Append("\"]")
    .Append(")") // ... and this
    .Append(") as ")
    .Append(name);

如果你的列不是<代码> int <代码>类型,我相信代码> toINT64 64 /代码>(<代码> Bigint < /Cube >),以及 >双()/代码>和<代码> toCIMIALL()/<代码> ./p> IMO,你在动态LINQ下得到的兔子洞越远,你就越应该考虑掌握表达式树的发音。

selectSB.Append("Sum(")
    .Append("Convert.ToInt32(") // Add this...
    .Append("it[\"")
    .Append(name)
    .Append("\"]")
    .Append(")") // ... and this
    .Append(") as ")
    .Append(name);