当前位置: 代码迷 >> 综合 >> List<T> 的ToDictionary使用
  详细解决方案

List<T> 的ToDictionary使用

热度:66   发布时间:2024-02-07 07:48:36.0

举个栗子

 

类定义

 public class test{public test(int _id,int _age,string _name){id   = _id;age  =_age;name =_name;}public int id;public int age;public string name;}
List<test> t=new List<test>();
t.Add(new test( 1, 2, "asd1"));
t.Add(new test( 2, 2, "esd2"));
t.Add(new test(3, 2, "we2" ));
t.Add(new test( 4, 2, "sa")); 
List<string>  test2=   t.Select(x => x.name).ToList(); 
Dictionary<int, string> dicname=  t.ToDictionary(x => x.id, y => y.name);

 

  相关解决方案