c# - How can I filter List property -
on view have autosuggestbox(searchfield) , listview, listview's itemsource bounded vm class property:
private class1 _searchmatches; public class1 searchmatches { { return _searchmatches; } set { this.set(ref _searchmatches, value); } }
on class1 have loaditems task:
async task> loaditems()
var stocks = _response.products? .select(s => new myclass(plservice.dtotomodel(s))) .tolist(); var items = stocks.groupby(p => p.productmodel.description) .select(p => p.first()) .tolist(); return items;
when type test on autosuggestbox , hit enter, simplest way filter items where(item.description == searchterm)? filter , update itemsource, not rewriting property
you can use <searchbox>
, it's querysubmitted
event. work <textbox>
too. if need refilter items
- create 2 lists, 1 store items
, items displaying.
here <searchbox>
sample:
private list<myclass> _items; // store items private list<myclass> _displayitems; public list<myclass> displayitems // list show { { return _displayitems; } set { setproperty(ref _displayitems, value); } } private void searchboxquerysubmitted(searchboxquerysubmittedeventargs eventargs) { searchterm = eventargs.querytext?.trim(); filter(searchterm); } private void filter(string searchterm) { if (_items == null) return; iqueryable<myclass> items = _items.asqueryable(); if (!string.isnullorempty(searchterm)) { searchterm = searchterm.tolower(); items = items.where(x => x.productmodel.description.tolower().contains(searchterm)); } displayitems = items.tolist(); }
Comments
Post a Comment