如何将选定的列添加到词典中? [英] How can I get selected column into a Dictionary?

查看:83
本文介绍了如何将选定的列添加到词典中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var topLevel= resultRows.GroupBy(g => g["CustomerAccountType"]);

我只想选择列,例如Actual1Actual2Actual3等.这些列的名称不是预先知道的-它们是动态组成的.

I want to select columns only like Actual1, Actual2, Actual3, etc. The names of the columns are not known upfront - they are composed dynamically.

我没有使用强类型的对象,因为resultsRows是从SQL多维数据集构造的Dictionary.

I am not using a strongly type object because resultsRows is a constructed Dictionary from SQL cubes.

我想动态地将ActualN ... to..ActualN作为字典添加到集合中.

I want to dynamically add ActualN...to..ActualN to a collection as dictionary.

推荐答案

您可以使用Select进行Actual1, Actual2, Actual3的投影:

You can use Select to make a projection of Actual1, Actual2, Actual3:

var topLevel= resultRows
    .Select(d => new Dictionary<string,object> {
        {"Actual1", d["Actual1"]}
    ,   {"Actual2", d["Actual2"]}
    ,   {"Actual3", d["Actual3"]}
    });

这将从字典中删除除Actual1, Actual2, Actual3以外的所有列.

This will remove all columns other than Actual1, Actual2, Actual3 from your dictionary.

我希望动态添加Actual1ActualN而不知道有多少

I want Actual1 to ActualN added dynamically without knowing how many are there

您也可以这样做:

// Construct this list dynamically
var selection = new List<string> {
    "Actual1", "Actual2", ..., "ActualN"
};
var topLevel= resultRows.Select(d => selection.ToDictionary(s => s, s => d[s]));

这篇关于如何将选定的列添加到词典中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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