如何将选定的列添加到词典中? [英] How can I get selected column into a Dictionary?
问题描述
var topLevel= resultRows.GroupBy(g => g["CustomerAccountType"]);
我只想选择列,例如Actual1
,Actual2
,Actual3
等.这些列的名称不是预先知道的-它们是动态组成的.
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.
我希望动态添加
Actual1
至ActualN
而不知道有多少
I want
Actual1
toActualN
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屋!