NicoTing NicoTing - 1 year ago 99
C# Question

How can I query ObservableCollection List

I have a property on my VM, and it is bounded to ListView

private FindCollection _searchMatches;
public FindCollection SearchMatches {
get { return _searchMatches; }
set { this.Set(ref _searchMatches, value); }
}


my "FindCollection" class inherited ObserveCollection:

FindCollection : ObservableCollection<MyClass>


On My FindCollection I have a LoadItems Task, which is called every time navigated to my view:

async Task<IList<MyClass>> 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;


So SearchMatches now have items

When user search for string , I want to sort the List Items , where description is equal to searchterm
How to query it to
SearchMatches.Where(s.description == searchterm)
to my VM;

UPDATE: SearchMatches contains {MyClass}, and MyClass{eachproduct}

Thanks,

NicoTing

Answer Source
var query  = SearchMatches.Where(s => s.Description.ToLower().Contains(searchTerm.ToLower()));
        FindCollection collection = new FindCollection();
        foreach (var r in query )
        {
            collection.Add(r);
        }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download