Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Linq作为IF条件_C#_Linq_If Statement - Fatal编程技术网

C# 如何使用Linq作为IF条件

C# 如何使用Linq作为IF条件,c#,linq,if-statement,C#,Linq,If Statement,我在数据库中搜索了一个客户,得到了一个小linq查询: var query = from c in database.customer where c.ID == input select c; 每个客户都有一个ID,在这种情况下由用户“输入”设置 数据库中的所有客户均来自“德国”,但有些客户应以不同的国家值(“英格兰”而非“德国”)复制到数据库中 现在,在将它们直接添加到数据库之前,我想检查数据库中是否已经有该客户的“英语版本” if (query.Where(c =>

我在数据库中搜索了一个客户,得到了一个小linq查询:

var query =
   from c in database.customer
   where c.ID == input
   select c;
每个客户都有一个ID,在这种情况下由用户“输入”设置

数据库中的所有客户均来自“德国”,但有些客户应以不同的国家值(“英格兰”而非“德国”)复制到数据库中

现在,在将它们直接添加到数据库之前,我想检查数据库中是否已经有该客户的“英语版本”

if (query.Where(c => c.country == "England"))
   //do nothing
else 
   //add the customer with c.country = "England"
问题是这不是一个常规的if语句

是否有一种可能的方式来实现我想在这个if条件下表达的内容


谢谢

只需将条件更改为

if (query.Any(c => c.country.Equals("England")))
   //do nothing
else 
   //add the customer with c.country = "England"
Linq
method
Where
返回无法自动转换为
bool
值的
IEnumerable
值。
Any
All
等方法会根据集合中至少一个元素的条件是
true
还是所有元素的条件分别返回
true
false

您是在寻找
Any
而不是
Where