I use PredicateBuilder to create a search / filter section in my action. Here he is:
[HttpPost] public ActionResult Test(int? cty, string inumber, int? page) { var lstValues = db.TableName.Include(x => x.Table1) .Include(x => x.Table2) .Include(x => x.Table3) .ToList(); var predicate = PredicateBuilder.True<TableName>(); if (!string.IsNullOrWhiteSpace(inumber)) { predicate = predicate.And(x => x.Inumber == inumber); } if (!string.IsNullOrWhiteSpace(cty.ToString())) { predicate = predicate.And(x => x.CtyID == cty); } if (predicate.Parameters.Count > 0) { lstValues = db.TableName.AsExpandable().Where(predicate).ToList(); Session["Paging"] = lstValues; ViewBag.Paging = lstValues.ToPagedList(page ?? 1, 2); return View(lstValues.ToPagedList(page ?? 1, 2)); } else { return View(lstValues.ToPagedList(page ?? 1, 2)); } }
Under this line is PredicateBuilder.True<TableName>(); i get an evil expression
PredicateBuilder.True () is deprecated. Use PredicateBuilder.New instead.
But I tried PredicateBuilder.New<T> , and my predicate always gets the value 1 , even if I have no values ββfor the parameters, so cty and inumber are zero. This returns me an empty table .. when it should return all records by entering the else with return View(lstValues.ToPagedList(page ?? 1, 2)); .
When I use PredicateBuilder.True<T> , I get all the records returned when all parameters are zero.
Any idea on why this is? Any help is appreciated.
c # asp.net-mvc predicate
GTown-Coder
source share