我正在开发一个Asp.Net MVC应用程序,我是Linq和CodeFirst的新手。在我的控制器中,这是我写的操作:Lambda表达式来检查值是否为空或等于
public ActionResult Filter(int? PaperType , int? PaperGram , int? Brand)
{
var FilteredResult ;
if (PaperType.HasValue && PaperGram.HasValue && Brand.HasValue) {
FilteredResult = db.Stocks.where(m => m.PaperType == PaperType && m.PaperGram == PaperGram && m.Brand == Brand);
}
else if (PaperType.HasValue && PaperGram.HasValue) {
FilteredResult = db.Stocks.where(m => m.PaperType == PaperType && m.PaperGram == PaperGram);
}
else if (PaperType.HasValue && Brand.HasValue) {
FilteredResult = db.Stocks.where(m => m.PaperType == PaperType && m.Brand == Brand);
}
// and ifs continue to last
/*
.
.
.
.
*/
else {
FilteredResult = db.Stocks;
}
return View(FilteredResult);
}
但我知道这不是在Linq和Codefirst中最好的方法。那么,你能否更好地解决这个问题?
从这个问题我明白所有其他标准必须添加只有PaperType.HasValue所以你必须像我一样移动所有ifs(除非我错了......) –
在我看来,他需要这些patameters的不同组合 –