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>
.
To check if the list contains an element in Python, use the “in” operator. The “in” operator checks if the list contains a specific item or not. It can also check if the element exists on the list or not using the list.
A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type.
Just like strings store characters at specific positions, we can use lists to store a collection of strings. In this tutorial, we will get a string with specific values in a Python list.
if(DB.Countries.Any(c => c.Country == newAddedCountry))
{
// exists ..
}
You can use Enumerable.Any
method;
Determines whether a sequence contains any elements.
if( DB.Countries.Any( n => n.Country == newAddedCountry ))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With