c# - 如何在列表中使用 "Contain"?

标签 c# linq list

我有国家数据库喜欢,

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

我得到这个数据源,
   DB.Countries.ToList();

我想要做的是检查新添加的国家是否已经存在,
就是喜欢,
if(DB.Countries.ToList().Contains(newAddedCountry))
{
 ..............
}

但我不知道如何转换 newAddedCountry (字符串)到 System.Collections.Generic.List<Country> .

最佳答案

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

关于c# - 如何在列表中使用 "Contain"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16145660/

相关文章:

c# - 锁字典不断增长,不知如何清理?

c# - 协助重构 LINQ 方法

c# - 什么是 Identity Server 4 引发事件

c# - 用户输入限制,(空格、字母或数字, '-' 和 '_')c#

c# - ef 中的奇怪语法

c# - 使用 LINQ 将新的 BindingList 与主 BindingList 协调

java - 关于 ArrayList 的 NullPointerException?

python - 使用变量动态切片字符串

python - 如何缩短这个 Python 打印表达式?

c# - 在图像上绘制缩放图片