0
public ActionResult sortFilteredItem(string sortByValue,string brand,string category,int price=0 )
{
  var sortedData = (dynamic)null;
  if (sortByValue != "")
  {
    if(sortByValue == "ltoh")
    {
      sortedData = DB.Prouducts.where(x=> x.brandName == brand & x.catName == category & x.price == price).ToList();
    }

  }
 return View(sortedData);
}

how i can neglect if price=0 from query means that it does not make any impact on EF query because if price=0 the query does not returning expected output.Because i have not any record that has 0 price so the query is always returning null.

if(price != 0)
{
    sortedData = DB.Prouducts.where(x=> x.brandName == brand & x.catName == category & x.price == price).ToList();
}
else
{
    sortedData = DB.Prouducts.where(x=> x.brandName == brand & x.catName == category).ToList();
}

i have tried like this it is working good but that is lengthy process.if i have 4 or 5 more variable that,s optional so it is necessary to check null value first for working.Any recommendation ?

4
  • 1
    you don't use and operator correctly IMHO. You should use && instead of & Commented Sep 17, 2019 at 14:30
  • that,s not problem i always use single & operator so that,s not an issue. issue is only to check null and neglect from query as i asked above. Commented Sep 17, 2019 at 14:34
  • 2
    there is already an answer that will solve your problem AFAIK. But using bitwise operator when you don't mean to compare like that is a bad practice. I did not answer your question. That is why I am putting it here in the comments section. Happy coding Commented Sep 17, 2019 at 14:37
  • you are right i does not said that you are wrong i just tell to you that is not problem. so you give me suggestion to use && that.s great Commented Sep 17, 2019 at 14:41

2 Answers 2

4

You can use the fllowing logic;

sortedData = DB.Prouducts.where(x=> x.brandName == brand 
    && x.catName == category 
    && (price == 0 || x.price == price)) //use this approach for every optional param
    .ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

same problem again if price=0 the query always return null because i have no any record in DB that has 0 price.
@Shakir, this condition does not do that. it is doing like, if input price==0 then (price == 0 || x.price == price) returns true. So if the code above does not work, you should look somewhere elste to solve your problem
2

What you can do is apply filters only if the condition holds. Let's say you need to check catName and price. So:

var query = DB.Prouducts.Where(x=> x.brandName == brand);
if (category != null)
{
     query = query.Where(x => x.catName == category);
}

if (price != 0)
{
    query = query.Where(x => x.price == price)
}

sortedData = query.ToList();

Obviously you'll need one "if" per filter, but it is much better than considering all possible combinations.

2 Comments

so that is lengthy process if i have to check 4 to 5 variables null.so i have to check like this if(brandName != "" & catName != "" & price != ""){ some code}and so on it will make 5 to 6 condtions that,s is real proble.
@Shakir, perhaps i didn't explain the answer well enough. see update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.