C# 在Where子句的lambda表达式中使用if/else
我有这个搜索代码C# 在Where子句的lambda表达式中使用if/else,c#,asp.net,c#-4.0,C#,Asp.net,C# 4.0,我有这个搜索代码 public List<Tbl_Product> ProductSearch(DateTime startdate, DateTime enddate, string productname, int suply) { var q = _db.Tbl_Product .Where(model => model.DateReg == startdate &&
public List<Tbl_Product> ProductSearch(DateTime startdate, DateTime enddate, string productname, int suply)
{
var q = _db.Tbl_Product
.Where(model =>
model.DateReg == startdate &&
model.DateReg == enddate)
.Where(model =>
model.ProductName.Contains(productname))
.Where({});
}
我应该怎么做?我个人不会在Where子句中这样做,因为这意味着您正在将suply变量传递到数据库
var q = _db.Tbl_Product
.Where(model => model.DateReg == startdate
&& model.DateReg == enddate
&& model.ProductName.Contains(productname));
if(suply == 1)
{
q = q.Where(model => model.Suply > 0);
}
else
{
q = q.Where(model => model.Suply == 0);
}
然而,如果你真的坚持要一次完成这一切:
.Where(model => (suply == 1 && model.Suply > 0)
|| (suply != 1 && model.Suply == 0));
可能重复的
.Where(model => (suply == 1 && model.Suply > 0)
|| (suply != 1 && model.Suply == 0));