我有国家数据库喜欢,
Country
-------
England
Germany
Italy
...
Run Code Online (Sandbox Code Playgroud)
我得到这个数据源,
DB.Countries.ToList();
Run Code Online (Sandbox Code Playgroud)
我想要做的是检查新添加的国家是否已经存在,
只是喜欢,
if(DB.Countries.ToList().Contains(newAddedCountry))
{
..............
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何转换newAddedCountry(字符串)System.Collections.Generic.List<Country>.
Tim*_*ter 14
if(DB.Countries.Any(c => c.Country == newAddedCountry))
{
// exists ..
}
Run Code Online (Sandbox Code Playgroud)
你可以使用Enumerable.Any方法;
确定序列是否包含任何元素.
if( DB.Countries.Any( n => n.Country == newAddedCountry ))
Run Code Online (Sandbox Code Playgroud)