我希望函数在List中插入元素而不重复 [英] i want function to insert element in List without duplicates

查看:75
本文介绍了我希望函数在List中插入元素而不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

旧列表:

列表<   int  >  OLd =新列表<   int  > (){1,1,1,1,1,1,1,2,2}; 



列表<   int  > 新=新列表<   int  < span class =code-keyword>> (){1,2}; 

解决方案

环顾四周区别......

http://blogs.msdn.com/b/kaevans/archive/2010/09/02/fun-with-linq-and-distinct.aspx [ ^ ]

Linq Distinct的通用IEqualityComparer() [ ^ ]


尝试使用以下代码:



 List< int> OLd =  new 列表< int>(){ 1  1  1  1  1  1  1  2  2 }; 
List< int> New = OLd.Distinct()。ToList();


我建​​议创建一个添加值的方法,并在添加之前在方法中添加一个检查值。

old list :

List<int> OLd = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };


List<int> New = new List<int>() { 1, 2 };

解决方案

Look around at Distinct...
http://blogs.msdn.com/b/kaevans/archive/2010/09/02/fun-with-linq-and-distinct.aspx[^]
A Generic IEqualityComparer for Linq Distinct()[^]


try with the following code:

List <int> OLd = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };
List<int> New = OLd.Distinct().ToList();


I suggest to create a method for adding the values and add a check in the method before adding that value.


这篇关于我希望函数在List中插入元素而不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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