1

This is my model:

    public class ObsModel : BindableBase
    {


        private String _ObsName = string.Empty;
        public string ObsName
        {
            get { return this._ObsName; }
            set { this.SetProperty(ref this._ObsName, value); }
        }

        private String _ObsColor = string.Empty;
        public string ObsColor
        {
            get { return this._ObsColor; }
            set { this.SetProperty(ref this._ObsColor, value); }
        }

        private String _ObsValue = string.Empty;
        public string ObsValue
        {
            get { return this._ObsValue; }
            set { this.SetProperty(ref this._ObsValue, value); }
        }
}

I can then populate my model using this object.

ObservableCollection<ObsModel> LastObsResults = new ObservableCollection<ObsModel>();

But how can I remove a particular collection if I have for example the observation name as a string variable by using the code below?

LatsObsResults.Remove( What DO I PUT HERE);
user3929914
  • 151
  • 1
  • 2
  • 11
  • Overide the `Equals` and `GetHashCode` methods that compare `_ObsName – KV Prajapati Apr 22 '15 at 13:45
  • Ok just to make sure, you have the `ObsModel.ObsName` and want to remove the corresponding `ObsModels` with that name? – ryanyuyu Apr 22 '15 at 13:46
  • yes I want to remove everything associated with that obsname – user3929914 Apr 22 '15 at 13:52
  • Possible duplication (with solution): http://stackoverflow.com/questions/5118513/removeall-for-observablecollections This also avoids recreate the collection and allow you to remove one or a list of elements. – Max Bündchen Apr 22 '15 at 13:59

4 Answers4

1

You remove an object from the collection by passing in the object to remove. This can be done through Linq like so:

LastObsResults.Remove(LastObsResults.FirstOrDefault(obs => obs.ObsName == name));

Cros
  • 4,367
  • 9
  • 41
  • 47
0

This solution uses LINQ .Where to produce the valid subset of your results, then assigns that subset to a new ObsevableCollection. I am using the constructor that accepts an IEnumerable.

var validSubset = LastObsResults.Where(obs => obs.ObsName != name);
LastObsResults = new ObservableCollection<ObsModel>(validSubset);
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
0

Try this:

var lastObsUpdated = LastObsResults.Except(model => model.ObsName == "Whatever")
                                   .ToList()
                                   .AsObservable();

This will create you an updated ObservableCollection with all but a named item.

James Lucas
  • 2,452
  • 10
  • 15
-1

If you want to remove all items you can use this:

LastObsResults .Clear();

If you want to create your own extension method you could do something like this:

public static class ObservableCollectionExtensions
{
    public static int Remove<T>(
        this ObservableCollection<T> coll, Func<T, bool> condition)
    {
        var itemsToRemove = coll.Where(condition).ToList();

        foreach (var item in itemsToRemove)
        {
            coll.Remove(item);
        }

        return itemsToRemove.Count;
    }
}

With the extension above you can remove all items that match the condition passed. Example:

LastObsResults.Remove(x=> x.ObsName == myName);

And finally, you could use the existing collection.Remove() method but make sure you use it like described here to avoid a very common issue.

Ricardo Sanchez
  • 6,079
  • 3
  • 24
  • 31