Q:

C# program to check a specified city exists in the List collection using Linq

0

C# program to check a specified city exists in the List collection 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 check a specified city exists in the List collection using Linq, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to check specified city exists 
//in the List Collection using Linq.

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

class LinqDemo
{
    static void Main(string[] args)
    {
        List<string> listCity = new List<string>() { "Delhi", "Mumbai", "Agra", "Gwalior", "Jhashi" };

        string city = "Agra";
        bool isExist = false;
        
        isExist = listCity.AsEnumerable().Contains(city);

        if (isExist == true)
            Console.WriteLine(city + " city exists in List");
        else
            Console.WriteLine(city + " city does not exists in List");
    }
}

Output:

Agra city exists in List
Press any key to continue . . .

Explanation:

In the above program, we created a class LinqDemo that contains the Main() method. In the Main() method we created a list listCity that contains the name of cities, and we also created a local variable city that is initialized with "Agra".

isExist = listCity.AsEnumerable().Contains(city);

In the above code Contains() method check the city "Agra" is exist in the list listCity. The listCity contains "Agra" that's why the Contains() method will return true and then the "Agra city exists in List" message will print on the console screen.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now