我相信有更好的方法来写这个,但我正在经历一个心理障碍。使用OR运算符的Lambda表达式
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 ...' – Default
谢谢大家的快速评论和回答。我没想到所有这些答案都会如此迅速地发生,但我想这就是为什么这是第一。 @默认谢谢你的提示。我一定会使用它。再次感谢 –
@Servy好点,我错过了。删除评论以避免混淆 –