Reputation: 43
I have a list that I want to filter based on the user input.
So what I've got is this:
var fullList = BLogic.GetDataStoreCompaniesForFilterList();
var filterList = fullList
.Where(w =>
w.Name.ToLower().StartsWith(filterString.ToLower()) || filterString.Length > 2 &&
w.Vat.ToLower().Contains(filterString.ToLower()) || w.IndustryLang != null &&
w.IndustryLang.Where(ww => ww.LanguageId == usrX.LanguageId)
.Select(s => s.Name.ToLower())
.Contains(filterString.ToLower())
).ToList();
more specifically the last part of the filter query is what's giving me troubles:
w.IndustryLang != null &&
w.IndustryLang.Where(ww => ww.LanguageId == usrX.LanguageId)
.Select(s => s.Name.ToLower()).Contains(filterString.ToLower())
The full list is list of objects that contains an ID, Name, Vat and a possible list (hence the null check) of IndustryLang objects. Such an IndustryLang object contains an ID, a LanguageID to check which language it is and a Name.
What I need is to be able to filter on the Name of an IndustryLanguage.
It probably is just a small mistake but I just don't seem to find it. Any help is appreciated! Thanks.
Upvotes: 1
Views: 77
Reputation:
I may be misinterpreting your question, and I am new to Linq. But isn't the list of languages at this point just the list that belongs to that particular company. It seems to me the condition to test for the language to contain your filter string shoul dbe more like so:
w.IndustryLang != null &&
w.IndustryLang.Select(s => s.Name.ToLower()).Contains(filterString.ToLower())
Upvotes: 0
Reputation: 1391
If you want to get the entries in fullList for which at least one IndustryLang has a name that contains the filtering condition, replace the 'where' clause by 'any' and include the condition on the IndustryLang object like so:
var fullList = BLogic.GetDataStoreCompaniesForFilterList();
var filterList = fullList
.Where(w =>
w.Name.ToLower().StartsWith(filterString.ToLower()) || filterString.Length > 2 &&
w.Vat.ToLower().Contains(filterString.ToLower()) || w.IndustryLang != null &&
w.IndustryLang.Any(ww =>
ww.LanguageId == usrX.LanguageId &&
ww.Name.ToLower().Contains(filterString.ToLower()))
).ToList();
Upvotes: 2
Reputation: 11
Is this what you mean?
var fullList = BLogic.GetDataStoreCompaniesForFilterList();
var filterList = fullList.Where(
( w => w.Name.ToLower().StartsWith(filterString.ToLower()) ) ||
( filterString.Length > 2 && w.Vat.ToLower().Contains(filterString.ToLower()) ) ||
( w.IndustryLang != null && w.IndustryLang.Where(ww => ww.LanguageId == usrX.LanguageId).Select(s => s.Name.ToLower()).Contains(filterString.ToLower()))).ToList();
I added some parenthesis.
Upvotes: 1