Q:

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

belongs to collection: C# LINQ Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# LINQ Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to print the employees whose salary is ... >>
<< C# program to print the numbers greater than 786 i...