迭代一些字典项 [英] iterate a number of dictionary items

查看:130
本文介绍了迭代一些字典项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字典:

 字典< string,List< string>> myList = new Dictionary< string,List< string>>(); 

这个字典被填充,我永远不会知道我将有多少项目。 p>

输出的一个例子:
词典元素(Key,Value)

  {Code,{Test1,Test2,Test3}} 

{Desc,{Desc1,Desc2 }}

{Name,{name1,name2,name3}}

如何循环遍历每个字典,并获取值,例如索引



将产生的东西 - >

  {Code =Test1,Desc =desc,Name =name1} 
{Code =Test2,Desc = desc2,Name =name2}
{Code =Test3,Desc =desc3,Name =name3}

任何想法?



谢谢

解决方案

关键是为数据的每一列构建一组数组,而不是像Dictionary枚举器将为您提供的每一行。这可以通过使用Aggregate扩展方法和一个简单的Select语句来实现。

  //假设以下类为目的地类型
class Foo
{
public Foo(string [] values)
{
代码=值[0];
Name = values [1];
Desc = values [2];
}

公共字符串代码;
public string Name;
public string Desc;
}

//这将是解析数据所需的代码
var destination = dataSource [Code]。Aggregate(new List&Foo>(),(条目,_)=>
{
var curentRow = entries.Count();

var entryData = dataSource.Select(property => property.Value [curentRow] ).ToArray();

entries.Add(new Foo(entryData));

返回条目;
});

在这种情况下,我们使用代码财产作为关键,以确定您的数据源(您的字典)中有多少条目。如果您的字典中有行缺少值(少于代码行中的项目),则此代码将失败,因为它假定所有行中的项目数量相同。



在这种情况下, Aggregate 方法就像一个for循环,为我们提供了一个名为 currentRow 我们稍后将使用您的数据访问特定条目。这个计数器是我们存储到 List< Foo> 中的条目数量。它从0开始,并且每次在结果集中添加一个新值时都会递增。



下一步是查看数据源中的所有条目,访问与当前行匹配的值。然后,我们将其转换为数组,并将其馈送到目标类型的构造函数中,该类型知道如何反序列化此数据。


I have the following Dictionary:

 Dictionary<string, List<string>> myList = new Dictionary<string,List<string>>();

This dictionary gets populated, I'm never going to know how many items I will have.

An example of the output: Elements of Dictionary: (Key,Value)

 {"Code",{"Test1", "Test2", "Test3"}}

 {"Desc",{"Desc1", "Desc2", "Desc3"}}

 {"Name",{"name1", "name2", "name3"}}

How can I loop through every dictionary and get the value i.e. by index

something that would yield ->

{Code = "Test1", Desc = "desc", Name = "name1"}
{Code = "Test2", Desc = "desc2", Name = "name2"}
{Code = "Test3", Desc = "desc3", Name = "name3"}

Any ideas?

Thanks

解决方案

The key is to build a set of arrays for each column of your data instead of for each rows like the Dictionary enumerator will provide you. This can be achieved through the use of the Aggregate extension method and a simple Select statement.

// Assuming the following class as a destination type
class Foo
{
    public Foo(string[] values)
    {
        Code = values[0];
        Name = values[1];
        Desc = values[2];
    }

    public string Code;
    public string Name;
    public string Desc;
}

// This would be the code required to parse the data
var destination = dataSource["Code"].Aggregate(new List<Foo>(), (entries, _) => 
{
    var curentRow = entries.Count();

    var entryData = dataSource.Select(property => property.Value[curentRow]).ToArray();

    entries.Add(new Foo(entryData));

    return entries;
});

In this case, we use the Code property as a key to figure out how many entries there are in your data source (your dictionary). If there are rows in your dictionary with missing values (less items than in the Code row), this code will fail as it assumes that there are the same amount of items in all the rows.

The Aggregate method acts like a for loop in this case, providing us with a basic counter named currentRow that we will use later to access specific entries in your data. This counter is the amount of entries we stored into the List<Foo>. It starts at 0 and it increments each time we add a new value to the result set.

The next step is to look at all the entries in your data source and to access the value that matches the current row. We then convert it into an array and feeds it into the constructor of your destination type that knows how to deserialize this data.

这篇关于迭代一些字典项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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