Search code examples
linqxamarinxamarin.formsobservablecollectiongrouped-collection-select

how to update observable collection group


I have implemented group observable collection like below.

Grouped Property

private ObservableCollection<Grouping<String, Request>> _groupedList = null;
public ObservableCollection<Grouping<String, Request>> GroupedList {
    get {
        return _groupedList;
    }
    set {
        _groupedList = value;
        RaisePropertyChanged(() => GroupedList);
    }

}

Creating List

var list = new List<Request>();

var grouped = from Model in list
                         group Model by Model.Done into Group
                         select new Grouping<string, Request>(Group.Key, Group);

GroupedList = new ObservableCollection<Grouping<string, TModel>>(grouped);

Now i need to update one item in the list without reloading full list for performance.

i did tried like this , mylist.FirstOrDefault(i => i.Id== mymodel.Id); Not worked for me.

I need to pick that particular item and edit and update into the list again using linq or something but i stuck here for group observable collection no efficient details to do this., anybody having idea about this help please.

And finally i get updated single item, but i need to do that without

GroupedList = new ObservableCollection<Grouping<string, TModel>>(grouped);

Because everytime it create new list and bind into my view.Thats again big performance though. Thanks in advance.


Solution

  • What I understand from your question is that you want to push an updated group without overwriting the entire ObservableCollection.

    To do that:

    var targetGroup = GroupedList.FirstOrDefault(i => i.Id == mymodel.Id);
    var targetIndex = GroupedList.IndexOf(targetGroup);
    var modifiedGroup = ... //Do whatever you want to do
    GroupedList[targetIndex] = modifiedGroup;
    

    This will trigger a 'replace' operation of the target grouping.