How to Merge string Arrays and keep distinct values using LINQ in C# ?

Do you want to merge string arrays and yet want to keep the distinct values in C# ? . One of the option to do this is using the Union extension method.

Below is a sample code snippet demonstrating how to do it.

How to Merge string Arrays and keep distinct values using LINQ in C# ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GinktageConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            MergeArray();
            Console.ReadLine();
        }
        public static void MergeArray()
        {
            string[] movies1 = new string[] { "Kaththi", "Yennai Arindhaal", "Lingaa" };
            string[] movies2 = new string[] { "Kaththi", "Jilla", "Veeram" };
            string[] result = movies1.Union(movies2).ToArray();
            foreach (var item in result)
                Console.WriteLine(item);
        }
    }
}

image

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...