Question

Below is my method of searching using predicate builder there is no error showing in visual studio but the problem is that the below code is not executing.

public JsonResult GetSearchedGraph(string searchItem, string itemTypeEnum)
        {

            var pre = PredicateBuilder.True<Graph>();
           pre.And(m => m.isHidden == false && m.ItemType!="FOLDER");
            if (!String.IsNullOrEmpty(searchItem))
           {
               pre.And(m => m.GraphItemTitle.ToUpper().Contains(searchItem.ToUpper()));
            }
            if (!String.IsNullOrEmpty(itemTypeEnum))
            {
                pre.And(m => m.ItemType == itemTypeEnum);
            }

            var searchGraph = from m in db.Graphs.AsQueryable() select m;
           searchGraph = db.Graphs.Where(pre);

           return Json(searchGraph.ToList(), JsonRequestBehavior.AllowGet);
        }

I am not getting any search result by using this method what it is wrong with this code?

Was it helpful?

Solution

well, you just have to do correct assignments.

because pre.And() doesn't impact pre

var pre = PredicateBuilder.True<Graph>();
//assign result of pre.And(xxx) to pre
pre = pre.And(m => m.isHidden == false && m.ItemType!="FOLDER");
if (!String.IsNullOrEmpty(searchItem))
{
     //same
     pre = pre.And(m => m.GraphItemTitle.ToUpper().Contains(searchItem.ToUpper()));
}
if (!String.IsNullOrEmpty(itemTypeEnum))
{
    //same
    pre = pre.And(m => m.ItemType == itemTypeEnum);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top