zey zey - 1 month ago 4
C# Question

How to use "Contain" in List?

I have country database likes ,

Country
-------
England
Germany
Italy
...


I get this data source as ,

DB.Countries.ToList();


What I want to do is to check new added country is already exists or not ,

just likes ,

if(DB.Countries.ToList().Contains(newAddedCountry))
{
..............
}


But I don't know how to convert
newAddedCountry
(string) to
System.Collections.Generic.List<Country>
.

Answer
if(DB.Countries.Any(c => c.Country == newAddedCountry))
{
    // exists ..
}
Comments