0

Three conditions in linq where clause using lambda expressions

List<Tbl_MVPConsultant> _objConsultants = _datalayer.Get_MVP_Consultants();

 _objConsultants = _objConsultants.Where(p => p.Country.ToLower().Contains(SearchTextbox.ToLower()) ||
                    p.State.ToLower().Contains(SearchTextbox.ToLower()) ||
                    p.City.ToLower().Contains(SearchTextbox.ToLower())).ToList();

I'm trying to achieve a filter operation three times using the above query .. but i'm getting an error stating object reference not set to an instance of an object ..

Looking for a quick solution.Appreciate early efforts. Thank you

4
  • are you sure _objConsultants has data? check out this link: stackoverflow.com/questions/2281083/… Commented Oct 14, 2014 at 13:59
  • Yeah _objConsultants has data and the _objConsultants = _objConsultants.Where(p => p.Country.ToLower().Contains(SearchTextbox.ToLower()) || p.State.ToLower().Contains(SearchTextbox.ToLower())).ToList(); I was able to fetch the results based on the two conditions but fails for the three condition statement.. @Niranjan Kala Commented Oct 15, 2014 at 7:04
  • @user3759894: Have you tried jon's suggested solution?? Commented Oct 15, 2014 at 7:26
  • Yes you and Jon were both right about it. There are some null values in the table. How to apply the checking of nulls at the start of lambda, @Niranjan Kala ? I'm very new to Linq ... Commented Oct 15, 2014 at 14:19

1 Answer 1

1

You won't be able to call p.Country.ToLower if p.Country is null. Put p.Country != null && p.State != null && p.City != null && at the start of the lambda.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right about it, there are some null values How to apply the checking of nulls at the start of lambda. can you re write my query @jon

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.