C# 操作员'||';无法应用于类型为';字符串';和';布尔';

C# 操作员'||';无法应用于类型为';字符串';和';布尔';,c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,我不知道为什么我在linq中编译我的查询,如下所示 from c in PriceListPolicies_TBLs where ((c.CountryCode ?? "VNALL")== "VNALL" ? "VN" : c.CountryCode || (c.CountryCode ?? "THALL") == "THALL" ? "TH" : c.CountryCode) select c 给出了这个错误 运算符“| |”不能应用于“string”和“bool”类型的操作

我不知道为什么我在linq中编译我的查询,如下所示

from c in PriceListPolicies_TBLs
where ((c.CountryCode ?? "VNALL")== "VNALL" ? "VN" : c.CountryCode || 
      (c.CountryCode ?? "THALL") == "THALL" ? "TH" : c.CountryCode) 
select c
给出了这个错误

运算符“| |”不能应用于“string”和“bool”类型的操作数


如何使此查询工作?

运算符只能应用于
bool
bool

c.CountryCode || (c.CountryCode ?? "THALL") // is wrong, since c.CountryCode is a string
试试这个:

from c in PriceListPolicies_TBLs 
where 
(
  ((c.CountryCode ?? "VNALL") == "VNALL" ? "VN" : c.CountryCode)
  || 
  ((c.CountryCode ?? "THALL") == "THALL" ? "TH" : c.CountryCode)
) 
select c
根据你的情况,你不想要条件。只需这样做:

var allItems = from c in PriceListPolicies_TBLs
               select c;

foreach (var c in allItems)
{
    if (c.CountryCode == "VNALL")
    {
        c.CountryCode = "VN";
    }
    else if (c.CountryCode == "THALL")
    {
        c.CountryCode = "TH";
    }
}

你完全断章取义。虽然这确实是代码中令人不快的部分,但您似乎不理解OP实际上试图做什么。有关详细信息,请参阅sll的答案。@sll感谢sll我尝试应用您的代码,但出现了以下错误:运算符“| |”无法应用于“string”和“string”类型的操作数……我正在尝试计算这一行。非常感谢,此条件没有意义。您基本上是在编写
where(someString1 | | someString2)
这是另一个问题,请添加您的答案,因为这是错误的,正如我解释的,JayJay通过它产生的编译器错误所显示的。我想不出这个条件有什么意义。你不能说
where(someString1 | | someString2)
。请解释您要查询的内容。@cremor您好,我只想在数据库的CountryCode字段中包含数据“VNALL”和“THALL”,然后在数据网格上显示缩写“VN”和“TH”,这是我希望的结果。希望您理解。不管怎样,Thansk