2012-07-13 64 views
7

我有一个Dictionary<string,int> dictionary1,我需要将其转换成List<Data>其中Data具有的属性lable = dictionary1.key和value = dictionary1.value。我不想使用for/foreach循环(我自己写的),因为为了避免它,我正在尝试使用Dictionary。“转换”字典<string,int>到列表<object>

另一种选择是有两个不同的字典(dictionary2和dictionary3),其中dictionary2<string,keyOfDictionary1>dictionary3<string,valueOfDictionary1>

我有道理吗?那可能吗?有更好的选择吗?

+6

“不想使用循环”将变得困难。你将要_have_迭代字典_somehow_。 – 2012-07-13 12:16:17

+2

您正在获得相当多的'LINQ'答案 - 请注意''LINQ'库仍然基本上运行'foreach'来返回结果。 – Oded 2012-07-13 12:18:33

+0

你如何看待linq的作品? – Jodrell 2012-07-13 12:19:07

回答

12

假设:

class Data 
{ 
    public string Label { get; set; } 

    public int Value { get; set; } 
} 

然后:

Dictionary<string, int> dic; 
List<Data> list = dic.Select(p => new Data { Label = p.Key, Value = p.Value }).ToList(); 
5

也许你可以使用LINQ?

dictionary1.Select(p => new Data(p.Key, p.Value)).ToList() 

这然而使用yield因而在背景环...

+2

这将需要'数据'有一个构造函数,将值作为参数,这在问题中没有指定。 – 2012-07-13 12:19:59

+0

@ErenErsönmez是的,但如果这是他所需要的,那么将它内联初始化是一件微不足道的事情。我指出了一种技术,而不是复制粘贴就绪的解决方案! – Leonard 2012-07-13 12:39:11

4
myDictionary.Select(x => new Data(){ label = x.Key, value = x.Value).ToList(); 
0
public class Data 
    { 
     public string Key { get; set; } 

     public int Value { get; set; } 
    } 

    private static void Main(string[] args) 
    { 
     Dictionary<string, int> dictionary1 = new Dictionary<string, int>(); 
     dictionary1.Add("key1", 1); 
     dictionary1.Add("key2", 2); 

     List<Data> data = dictionary1.Select(z => new Data { Key = z.Key, Value = z.Value }).ToList(); 

     Console.ReadLine(); 
    } 
1

尝试

dictionary1.Select(p => new Data(p.Key, p.Value)).ToList(); 
2

我认为 “没有循环”,其实就是 “我想LINQ”:

List<Data> = dictionary1.Select(
    pair => new Data() { 
     label = pair.Key, 
     value = pair.Value 
    })).ToList(); 
1

.NET已经有做什么Data会做一个数据类型:KeyValuePair<T1,T2>。字典已经实现了IEnumerable<KeyValuePair<T1,T2>>,只是投它。

Dictionary<string, int> blah = new Dictionary<string, int>(); 
IEnumerable<KeyValuePair<string, int>> foo = blah; 
+0

非常简单。如果不了解OP的需求,我们不知道它是否真的不需要比这更复杂。 :) – 2012-07-13 12:23:29

+0

我同意它不会满足需要,如果'键'*绝对*必须重命名'标签'。但这是避免循环AFAIK的唯一方法。但是,即使演员也毫无意义。 OP可以在任何需要IEnumerable的地方使用字典。 – 2012-07-13 12:29:30

+0

不幸的是我需要将我的数据重命名为Label。我试过这样做,但我会和你说的一样。所以,非常感谢你的帮助,我很抱歉,我没有明确地说我不想自己编写for/foreach循环,因为我知道内置的函数会为我更好地实现方式(即使在后台循环是必要的)。 – Jenninha 2012-07-13 13:44:13

1

这是一个老帖子,但只张贴到帮助他人的;)

实例转换任何对象类型:

public List<T> Select<T>(string filterParam) 
{  
    DataTable dataTable = new DataTable() 

    //{... implement filter to fill dataTable } 

    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); 
    Dictionary<string, object> row; 

    foreach (DataRow dr in dataTable.Rows) 
    { 
     row = new Dictionary<string, object>(); 
     foreach (DataColumn col in dataTable.Columns) 
     { 
      row.Add(col.ColumnName, dr[col]); 
     } 
     rows.Add(row); 
    } 

    string json = new JavaScriptSerializer().Serialize(rows); 

    using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) 
    { 
     DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(T[])); 
     var tick = (T[])deserializer.ReadObject(stream); 
     return tick.ToList(); 
    } 
} 
0

以防万一,只是帮助的人,我做到了像这样 - 将处理比单个值类型更复杂的对象,如OP所述。

// Assumes: Dictionary<string, MyObject> MyDictionary; 
List<MyObject> list = new List<MyObject>(); 
list.AddRange(MyDictionary.Values.ToArray()); 
相关问题