ADVERTISEMENT
ADVERTISEMENT

C# program to print the names that contain 'MAN' substring using LINQ

Here, we are going to learn how to print the names that contain 'MAN' substring using LINQ in C#?
Submitted by Nidhi, on August 21, 2020

Here we will create an array of Names then we print only those names that contain "MAN" substring using LINQ and print them on the console screen.

Program:

The source code to print names that contain "MAN" substring using LINQ in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to print the names that contain 
//"MAN" substring using LINQ in C#

using System;
using System.Linq;

class Demo
{
    static void Main()
    {
        string []Names = {"SUMAN","RAMAN","KIRAN","MOHAN"};
        
        var strs = from s in Names
                   where s.Contains("MAN")
                   select s;
        
        Console.WriteLine("Names are :");
        foreach (string str in strs)
        {
            Console.WriteLine("{0} ",str);
        }
    }
}

Output:

Names are :
SUMAN
RAMAN
Press any key to continue . . .

Explanation:

In the above program, we created a class Demo that contains the Main() method.

string []Names = {"SUMAN","RAMAN","KIRAN","MOHAN"};

In the Main() method we created an array of Names

var strs = from s in Names
    where s.Contains("MAN")
    select s;

In the above code, we select names that contain "MAN" substring.

Console.WriteLine("Names are :");
foreach (string str in strs)
{
    Console.WriteLine("{0} ",str);
}

In the above code, we printed the names selected from the LINQ query on the console screen.

C# LINQ Programs »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

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.