本文介绍了从C#中的列表返回唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个应用程序,其中我从gridview获取ID并将其保存在整数列表List< int>中.
Hi all,
I have an application in which i get the id''s from a gridview and save it in an integer list List<int>.
List<int> list = new List<int>();
//Example say, if the values in my list is
list={100,100,101,124,155,114,101};
//I want my list to return only unique values as i am passing my list to a database for retrieving data.
//I want my list to return like this.
list={100,101,124,155,114};
我该怎么办.请帮助我....
How can i do this.Pls help me....
推荐答案
List<int> list = new List<int>();
list.Add(100);
list.Add(100);
list.Add(101);
list = list.Distinct().ToList();
现在它将有100和101
Now this will have 100 and 101
List<int> l = new List<int>() { 100, 100, 101, 102, 103, 102 };
var v = l.Distinct();
foreach (var item in v)
{
Debug.WriteLine(item.ToString());
}
// or
List<int> li = l.Distinct().ToList();
会给你:
Will give you:
100
101
102
103
HashSet<int> uniqueNumbers = new HashSet<int>(list);</int></int>
这将为HashSet提供唯一编号,在您的情况下仅包含5个值.
This will give a HashSet with unique number, in your case list of 5 values only.
这篇关于从C#中的列表返回唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!