Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0 list .NET4 , +
Hi all,
I have an application in which i get the id's from a gridview and save it in an integer list List.
 
  
 
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....
Posted 10-Oct-12 0:36am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Check this
 
List<int> list = new List<int>();
            list.Add(100);
            list.Add(100);
            list.Add(101);
            list = list.Distinct().ToList();
 
Now this will have 100 and 101
  Permalink  
Comments
ajithk444 at 10-Oct-12 7:28am
   
thank U.:)it helped:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Why not using LINQ ?
 
 
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
  Permalink  
v2
Comments
TheCoolCoder at 10-Oct-12 6:12am
   
Was abt to post this and thought about refreshing :-)..
Andy411 at 10-Oct-12 6:18am
   
:-)
ajithk444 at 10-Oct-12 7:28am
   
thank U.:)it helped:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Either you can write your own algorithm or if you are using .Net 3.5 and above use HasSet.
 
Once you have your values in list, use following code
 
HashSet<int> uniqueNumbers = new HashSet<int>(list);</int></int>
 
This will give a HashSet with unique number, in your case list of 5 values only.
  Permalink  
Comments
ajithk444 at 10-Oct-12 7:28am
   
thank U.:)it helped:)
Milind Thakkar at 10-Oct-12 8:42am
   
Glad that it helped
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

A possibility for your own function (as said in solution 1)
 
You can start copying the first element to the second list and then delete it with RemoveAt, then use the Findcommand with that element. If found... remove it and try to find it another time, If not found start from the beggining with the next item to be copied to the second list.
 
You can use the FindAll and RemoveAll as well
 
Have a look
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^]
  Permalink  
v3
Comments
ajithk444 at 10-Oct-12 7:28am
   
thank U.:)it helped:)
Nelek at 11-Oct-12 7:06am
   
You are welcome. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

THIS IS HOW I DID in a big complicated manner though.Anyway thanks for all ur feedback.It helped a lot.
 
public List<int> GetEmpID() //Get unique ID's in a list 
    {
        List<int> list = new List<int>();
        List<int> Newlist = new List<int>();
        foreach (GridViewRow gvr in this.gridBWAProject.Rows)
        {
            if (((System.Web.UI.WebControls.CheckBox)gvr.FindControl("chkSelect")).Checked == true)
            {
                list.Add(Convert.ToInt32(gvr.Cells[1].Text.ToString()));
            }
        }
        
        IEnumerable<int> distinctlist = list.Distinct();
        foreach (int id in distinctlist)
       {
        Newlist.Add(id);
       }
        return Newlist;
  Permalink  
v3
Comments
Milind Thakkar at 10-Oct-12 8:43am
   
You can replace IEnumerable distinctlist = list.Distinct(); by newList = IEnumerable distinctlist = list.Distinct().ToList();
Milind Thakkar at 10-Oct-12 8:43am
   
That will save you a foreach loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 PIEBALDconsult 110
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100