ADVERTISEMENT
ADVERTISEMENT

C# program to demonstrate the example of Linq Union() method with StringComparer

Here, we are going to learn about the Linq Union() method with StringComparer and its C# implementation with an example.
Submitted by Nidhi, on August 28, 2020

Here we will perform union operation between two lists by ignoring the case using Linq Union() method with StringComparer and print the result on the console screen.

Program:

The source code to demonstrate the Linq Union() method with StringComparer, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to demonstrate Linq Union() method 
//with StringComparer.

using System;
using System.Linq;
using System.Collections.Generic;

class Demo
{
    static void Main(string[] args)
    {
        List<string> List1 = new List<string>() { "Abc", "Pqr", "Lmn", "Xyz" };
        List<string> List2 = new List<string>() { "Abc", "pqr" , "KLP" };

        var result = List1.Union(List2, StringComparer.OrdinalIgnoreCase);

        foreach (var value in result)
        {
            Console.WriteLine(value + " ");
        } 
    }
}

Output:

Abc
Pqr
Lmn
Xyz
KLP
Press any key to continue . . .

Explanation:

In the above program, we created two lists of integer numbers that are given below.

List<string> List1 = new List<string>() { "Abc", "Pqr", "Lmn", "Xyz" };
List<string> List2 = new List<string>() { "Abc", "pqr" , "KLP" };

Here we used Linq Union() method to perform union operation by ignoring case between two lists using the below code.

var result = List1.Union(List2, StringComparer.OrdinalIgnoreCase);

Then we print the result on the console screen using the below code using the "foreach" loop.

foreach (var value in result)
{
    Console.WriteLine(value + " ");
} 

C# LINQ Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.