C#从列表中生成嵌套字典

时间:2018-11-19 18:45:51

标签: c#

我设法使用实体框架将以下数据从数据库中提取到List<>中。

id      Variable        Value   Coef
--------------------------------------
1000    Gender          Male     0
1001    Gender          Female  -0.205
1009    College Code    AT      -1.732
1010    College Code    BU      -1.806
1011    College Code    EH      -1.728
1012    College Code    EN      -2.003
1013    College Code    LF      -1.779
1014    College Code    pp      -2.042
1015    College Code    SC      -2.070
1016    College Code    UC      -1.845
1017    AGI             AGI N/A  0.236
1018    AGI             0       -0.684

我对C#有点陌生,所以我想知道用以下格式创建嵌套Dictionary的最佳方法是什么:

//to construct a dictionary to hold Dictionary<Variable, {Value, Coef}>
Dictionary<string, Dictionary<string, double>> data = Dictionary<string, Dictionary<string, double>>();

例如,我可以这样访问数据:

Console.WriteLine(data['Gender']['Male']) //returns 0
Console.WriteLine(data['College Code']['LF']) //returns -1.779

3 个答案:

答案 0 :(得分:2)

var data = _dbContext.Tbl.ToDictionary(_ => _.Variable + "=" + _.Value, _ => _.Coef, StringComparer.OrdinalIgnoreCase);

使用显示的数据,无需分组,只需创建复合键并指定值即可。我还建议使用不区分大小写的键。

然后您将以College Code=LF为键的方式访问数据。

Console.WriteLine(data["College Code=LF"]);

答案 1 :(得分:2)

使用一些初始数据如下

public class data
{
    public int id { get; set; }
    public string Variable { get; set; }
    public string Value { get; set; }
    public decimal Coef { get; set; }
}

var listy = new List<data>() {
    new data() { id=1000, Variable="Gender", Value="Male", Coef=0m },
    new data() { id=1001, Variable="Gender", Value="Female", Coef=-0.205m },
    new data() { id=1009, Variable="College Code", Value="AT", Coef=-1.732m },
    new data() { id=1010, Variable="College Code", Value="BU", Coef=-1.806m },
    new data() { id=1011, Variable="College Code", Value="EH", Coef=-1.728m },
    new data() { id=1012, Variable="College Code", Value="EN", Coef=-2.003m },
    new data() { id=1013, Variable="College Code", Value="LF", Coef=-1.779m },
    new data() { id=1014, Variable="College Code", Value="pp", Coef=-2.042m },
    new data() { id=1015, Variable="College Code", Value="SC", Coef=-2.070m },
    new data() { id=1016, Variable="College Code", Value="UC", Coef=-1.845m },
    new data() { id=1017, Variable="AGI", Value="AGI N/A", Coef=0.236m },
    new data() { id=1018, Variable="AGI", Value="0", Coef=-0.684m },
};

获取变量字段的不同列表以播种外部词典,然后在数据源中找到相关项目,并为这些内容创建内部词典:

var b = listy
    .Select(x => x.Variable)
    .Distinct()
    // outer dictionary, key is Variable
    .ToDictionary(k => k, v => 
        listy
        // find items in the list with the same Variable
        .Where(x => x.Variable == v)
        // and create a dictionary for the Value/Coef pairs.
        .ToDictionary(k2 => k2.Value, v2 => v2.Coef));

一些交互式shell输出:

> b["AGI"]
Dictionary<string, decimal>(2) { { "AGI N/A", 0.236 }, { "0", -0.684 } }
> b["AGI"]["0"]
-0.684
> b["College Code"]["AT"]
-1.732
> b["College Code"]["BU"]
-1.806

答案 2 :(得分:1)

收集日期,您应该能够获得想要的结果。

var lookup = data.GroupBy(x => x.Variable)
    .ToDictionary(g => g.Key, g.ToDictionary(y => y.Value, y => y.Coef));

请注意,如果您在VariableValue列中有多个具有相同值的项目,则会失败。