简体   繁体   中英

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

Use AdvancedCollectionView from UWPCommunityToolkit. It will make your work very simple.

To use AdvancedCollectionView you should install Microsoft.Toolkit.Uwp.UI nuget package.

Here is the syndax:

var acv = new AdvancedCollectionView(oc);  //oc is your ObservableCollection
acv.Filter = s => s.Description.ToLower().Contains(searchTerm.ToLower());
YourListView.ItemsSource = acv;

For more info: AdvancedCollectionView

var query  = SearchMatches.Where(s => s.Description.ToLower().Contains(searchTerm.ToLower()));
        FindCollection collection = new FindCollection();
        foreach (var r in query )
        {
            collection.Add(r);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM