jQuery and ASP.NET

January 20, 2009

Remove Duplicate Items from a Generic List




If you have a List<string> and want to remove duplicate values before binding it to a control, here's how to do so

C#


var strList = new List<string> 

{ "Jane", "Bill", "jane", "carol", "Carol", "bill" }; 

strList = strList.ConvertAll(low => low.ToLowerInvariant());

 

var result = from str in strList

        group str by str into grp

        select new { Text = grp.Key};

foreach (var res in result)

{

    DropDownList1.Items.Add(res.Text);

}



VB.NET


    Dim strList = New List(Of String)(New String() _

                {"Jane", "Bill", "jane", "carol", "Carol", "bill"})

    strList = strList.ConvertAll _

    (Function(low) low.ToLowerInvariant())

 

    Dim result = _

     From str In strList _

     Group str By str Into grp = Group _

     Select New With {Key .Text = str}

    For Each res In result

        DropDownList1.Items.Add(res.Text)

    Next res



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Remove Duplicate Items from a Generic List"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions