Dathatreya
Dathatreya

Reputation: 45

Multiple conditions in linq lambda query

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

Upvotes: 0

Views: 5156

Answers (1)

Jon
Jon

Reputation: 126

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.

Upvotes: 1

Related Questions