Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "Contain" in List?

Tags:

c#

list

linq

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> .

like image 261
zey Avatar asked Apr 22 '13 11:04

zey


People also ask

Is there a Contains method for lists in Python?

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.

What can a list contain?

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.

Can a list contain string?

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.


2 Answers

if(DB.Countries.Any(c => c.Country == newAddedCountry))
{
    // exists ..
}
like image 51
Tim Schmelter Avatar answered Sep 29 '22 21:09

Tim Schmelter


You can use Enumerable.Any method;

Determines whether a sequence contains any elements.

if( DB.Countries.Any( n => n.Country == newAddedCountry ))
like image 26
Soner Gönül Avatar answered Sep 29 '22 21:09

Soner Gönül