用于检查'if'语句中的多个条件的语法

use*_*862 2 c#

这是情况:

if count items is either 0,1,5,7,8,9,10 then string = "string one"
if count items is either 2,3,4 then string = "string two"
Run Code Online (Sandbox Code Playgroud)

我试过(在cs razor视图内)

@if (@item.TotalImages == 1 || 5 || 7 || 8 || 9 || 10)
{
   string mystring = "string one"
}
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误

运营商|| 不能应用于bool或int类型的操作数

Adi*_*dil 7

您的语法或运算符错误.

改成.

@if (@item.TotalImages == 1 || @item.TotalImages == 5)
{
   string mystring = "string one"
}
Run Code Online (Sandbox Code Playgroud)


fae*_*ter 6

也许

var accepted = new HashSet<int>(new[] {1, 5, 7, 8, 9, 10});

@if (accepted.Contains(item.TotalImages))
{
   string mystring = "string one"
}
Run Code Online (Sandbox Code Playgroud)