C# Program to Check a Specified City Exists in the List Collection using LINQ - GeeksforGeeks

09 Dec, 2021
Given a list, we need to check if a specified city exists in the given list using LINQ. So we can do this task using contains() method. This method is used to check whether a sequence contains a specified element or not. The method overloads in the following ways:
Contains<TSource>(IEnumerable<TSource>, TSource): It is used to check whether the specified sequence or list holds the given element by using the default equality comparer.
Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>): It is used to check whether the specified sequence or list holds the given element by using a specified IEqualityComparer<T>.
Syntax:
public static bool Contains<TSource> (this System.Collections.Generic.IEnumerable<TSource> Mylist, TSource element);
Return Type: The return type is Boolean. It will return true if the given element is present in the list. Otherwise, it will return false.
Example:
Input  : ["Mumbai", "Pune", "Bangalore", "Hyderabad"]
search_city : "Mumbai"
Output : True

Input  : ["Chennai", "Vizag", "Delhi"]
search_city : "Hyderabad"
Output : False
Approach:
Create a method isPresent() that takes city list and name of the city to be searched as arguments.
In isPresent() method, Using contains() check if the city is present in list or not.
bool isExist = City_List.AsEnumerable().Contains(city);
If the city is present it will return true, else it will return false. By using an if condition check if it is true or false and print the result.
In main method, create a list of city names using arrayList.
Calling isPresent() method with two parameters one is list and another one is the name of city that we want to check is present or not.
isPresent(City_List, "Mumbai");
C#
// C# program to check if the given city // is available in the list or notusing System;using System.Collections.Generic;using System.Linq;  class GFG{      // Method to check the given city is present in the list or notstatic void isPresent(List<string> City_List, string city){    bool isExist = City_List.AsEnumerable().Contains(city);          if (isExist)        Console.WriteLine(city + " is present in list");    else        Console.WriteLine(city + " is not present in list");}  // Driver codestatic void Main(string[] args){          // Creating a list    List<string> City_List = new List<string>(){ "Mumbai", "Pune",                                                  "Bangalore", "Hyderabad" };          // Calling method    isPresent(City_List, "Mumbai");    isPresent(City_List, "Chennai");}}
Output:
Mumbai is present in list
Chennai is not present in list
Explanation: In the above example, first of all, we create an isPresent() method. In this method, we use contains() method to check if the city is present in the list or not and store the result in isExist variable. Now when the if statement is true then it will return “City_name is present in the list” otherwise return “City_name is not present in the list”. Now in the main method, we create a list that contains city names and then call isPresent() method with the city list and name of the city, that is isPresent(City_List, “Mumbai”); and isPresent(City_List, “Chennai”);. As the 1st specified city name is Mumbai and it is present in the list so it returned true, the second specified city name is Chennai which is not there in the list so it returned false.
CSharp LINQ
Picked
C#
C# Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Convert String to Character Array in C#
Getting a Month Name Using Month Number in C#
Program to Print a New Line in C#
Socket Programming in C#
Program to find absolute value of a given number