c# - Lambda expressions query null field -
well, have following query use search list text user has filled.
it's 1 textbox search in these fields below, it's working when 1 of these fields null, it's throwing null reference exception, how can avoid that?
list<rep_medidordisplay> searchfiltered = new list<rep_medidordisplay>(); if (filter != string.empty) { searchfiltered.clear(); foreach (string item in filter.split(';').tolist<string>()) { searchfiltered.addrange(medidores.where(x => x.data_toi.contains(item.trim()) || x.elemento.toupper().contains(item.trim()) || x.fase.toupper().contains(item.trim()) || x.id.toupper().contains(item.trim()) || x.kdke.toupper().contains(item.trim()) || x.n_equipamento.toupper().contains(item.trim()) || x.status.toupper().contains(item.trim()) || x.tensao.toupper().contains(item.trim()))); } }
i hope guys can me. thanks.
by checking null
first:
(x.elemento != null && x.elemento.toupper().contains(item.trim())) || // etc
of course should calculate item.trim()
once , reuse value tests rather trimming many times there fields.
Comments
Post a Comment