jQuery and ASP.NET

March 18, 2010

Join Two String Arrays with Distinct values using LINQ




One of my colleagues was looking out for a simple way to join two string arrays and avoid duplicates (if any) in those arrays. I asked him to use the Union method which excludes duplicates from the return set.

C#

static void Main(string[] args)
{
string[] arr1 = { "One", "Two", "Four", "Six" };
string[] arr2 = { "Three", "Two", "Six", "Five" };

var arr3 = arr1.Union(arr2);

foreach (string n in arr3)
{
Console.WriteLine(n);
}
Console.ReadLine();

}

VB.NET (option infer on)

Sub Main(ByVal args() As String)
Dim arr1() As String = { "One", "Two", "Four", "Six" }
Dim arr2() As String = { "Three", "Two", "Six", "Five" }

Dim arr3 = arr1.Union(arr2)

For Each n As String In arr3
Console.WriteLine(n)
Next n
Console.ReadLine()

End Sub

OUTPUT

Merge String Arrays LINQ



'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 "Join Two String Arrays with Distinct values using LINQ"
 

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