如何转换List< object>到词典< object,string>? [英] how convert List<object> to Dictionary<object, string>?

查看:118
本文介绍了如何转换List< object>到词典< object,string>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个

 List< object> dataList 





我需要转换为

 Dictionary< object,string> SearchData =  new 字典< object,string>(); 

解决方案

首先,不要创建对象的列表 - 它违背了Generic Collections的目的,即强类型。使用实际类类型的列表,或者如果Linq请求返回的话,则使用var。



然后只使用ToDictionary Linq方法:http://msdn.microsoft.com/en-us/library/bb549277(v=vs。 95).aspx [ ^ ]



列表< MyClass> mylist =  new 列表< MyClass> {
new MyClass(){Id = 208 ,Subject = some text},
new MyClass() {Id = 209 ,Subject = 另一个文字},
new MyClass(){Id = 210 ,Subject = 一个文字},
new MyClass(){Id = 211 ,Subject = 文字混合}};
Dictionary< int,MyClass> dict = mylist.ToDictionary(m = > m.Id);


  var  res = list.ToDictionary (x = >  x,x = >  x); 



第一个lambda让你选择密钥,第二个lambda选择值。



你可以玩它并使值与键,如下:



  var  res = list.ToDictionary( x = >  x,x = >   string .Format(  Val:{0},x)); 



如果您的列表包含重复项,请添加Distinct(),如下所示:



  var  res = list.Distinct()。ToDictionary(x = >  x,x = >  x); 



编辑为了评论有效原因,我认为对于这样的转换可能有效的唯一原因是,在某些时候,结果字典中的键和值将会发生分歧。例如,您将进行初始转换,然后使用其他值替换某些值。如果键和值总是相同,则HashSet< String>将提供更适合您的情况:



  var  res =  new  HashSet< string>(list); 
if (res.Contains( string1 ))


Hi All,

i have a

List<object> dataList



and i need to convert to

Dictionary<object, string> SearchData = new Dictionary<object, string>();

?

解决方案

First off, don't create lists of object - it defeats the purpose of Generic Collections, which is strong typing. Use a list of your actual class type, or a var if this is returned by a Linq request.

Then just use the ToDictionary Linq method: http://msdn.microsoft.com/en-us/library/bb549277(v=vs.95).aspx[^]

List<MyClass> mylist = new List<MyClass> {
           new MyClass(){Id = 208, Subject = "some text"},
           new MyClass(){Id = 209, Subject = "another text"},
           new MyClass(){Id = 210, Subject = "one text"},
           new MyClass(){Id = 211, Subject = "text mix"}};
Dictionary<int, MyClass> dict = mylist.ToDictionary(m => m.Id);


var res = list.ToDictionary(x => x, x => x);


The first lambda lets you pick the key, the second one picks the value.

You can play with it and make values differ from the keys, like this:

var res = list.ToDictionary(x => x, x => string.Format("Val: {0}", x));


If your list contains duplicates, add Distinct() like this:

var res = list.Distinct().ToDictionary(x => x, x => x);


EDIT To comment on the valid reason, I think the only reason that could be valid for conversions like this is that at some point the keys and the values in the resultant dictionary are going to diverge. For example, you would do an initial conversion, and then replace some of the values with something else. If the keys and the values are always going to be the same, HashSet<String> would provide a much better fit for your situation:

var res = new HashSet<string>(list);
if (res.Contains("string1"))


这篇关于如何转换List&lt; object&gt;到词典&lt; object,string&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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