数据源
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
var list = new List<TestClass> { new TestClass{Id=1,Name="1"}, new TestClass{Id=2,Name="2"}, new TestClass{Id=3,Name="3"}, new TestClass{Id=4,Name="4"}, new TestClass{Id=5,Name="5"}, new TestClass{Id=1,Name="9"}, };
1.使用HashSet去重再ToDictionary
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
var hash = new HashSet<int>(); var dict = list.Where(n=>!hash.Add(n.Id)).ToDictionary(key => key.Id, value => value.Name);
2.使用ToLookup(类似于一对多的字典)
var lookup = list.ToLookup(key => key.Id, value => value.Name);