【发布时间】:2016-06-14 10:18:05
【问题描述】:
我相信有更好的方法来写这个,但我遇到了心理障碍。
int num = 0;
using(var db = new TestDB())
{
num = db.Table.Where(x => x.FavoriteSport == "Baseball" &&
(x.FavoriteColor == "Green" ||
x.FavoriteColor == "Blue" ||
x.FavoriteColor == "Red")).Count();
}
return num;
有没有更好的方法来编写OR 语句?我试过了:
x.FavoriteColor == "Green" || "Blue" || "Red"
但是编译器说Operator || cannot be applied to operands of type 'bool' and 'string'
感谢任何帮助。
【问题讨论】:
-
追求可读性。我相信您当前的示例既可读又可维护。这没什么错。请注意如果
using和return语句之间没有任何内容,您可以执行return db.Table.Where... -
感谢大家的快速cmets和答案。我没想到所有这些答案都会如此迅速地发生哈哈,但我想这就是为什么 SO 是 #1 的原因。 @Default 感谢您的注意。我一定会使用它。再次感谢
-
@Servy 好点,我错过了。删除评论以避免混淆