Water
Water

Reputation: 1182

Cast IEnumerable to custom ObservableCollection Class?

Is it possible to convert IEnumerable to a Custom Class that is inherting from ObservableCollection class?

Reason is I want to select only a filtered set of items on the get. I want to implement it on the get because lots of other properties reference CustomItems and perform processes on the items, but I want to somehow make it process filtered set of items depending if a value is enabled or not.

Below is code to help explain what I want to achieve:

public class CustomItemsCollection : ObservableCollection<ItemViewModel>
{
    public ListView ListView { get; set; }
    public void ScrollToItem(object item = null)
    {
        //Some custom Code
    }
}

And here is my property that I want to customize:

private CustomItemsCollection _CustomItems = null;
    [JsonProperty]
    public CustomItemsCollection CustomItems
    {
        get
        {
            if (_CustomItems != null)
            {
                if(SomeValueIsEnabled)
                {
                    var filteredItems = _CustomItems.Where(c => c.Property.equals(SomeValue));
                    var castedItems = (CustomItemsCollection)filteredItems;
                    return castedItems;
                }
                return _CustomItems;
            }

            _CustomItems = new CustomItemsCollection();
            _CustomItemsChangedSource = new CollectionChangedWeakEventSource();
            _CustomItemsChangedSource.SetEventSource(_CustomItems);
            _CustomItemsChangedSource.CollectionChanged += _CustomItemsChangedSource_CollectionChanged;
            return _CustomItems;
        }
        set { _CustomItems = value; RaisePropertyChanged("CustomItems"); }
    }

Specifically, this part:

if(SomeValueIsEnabled)
{
    var filteredItems = _CustomItems.Where(c => c.Property.equals(SomeValue));
    var castedItems = (CustomItemsCollection)filteredItems;
    return castedItems;
}

Is this possible / or maybe wrong? What is the best practice to do it?

Thank you!

Upvotes: 2

Views: 426

Answers (2)

user2896170
user2896170

Reputation:

Try with this code:

 var filteredItems = _CustomItems.Where(c => c.Property.equals(SomeValue))
.Select(pre=> new ItemViewModel(){
//add info here
});
var castedItems = new CustomItemsCollection(filteredItems);

Upvotes: 0

Blorgbeard
Blorgbeard

Reputation: 103467

You can't just cast it, but you can create an instance of CustomItemsCollection and initialize it with filteredItems.

Add a constructor to your custom class that passes through to the appropriate ObservableCollection constructor:

public class CustomItemsCollection : ObservableCollection<ItemViewModel>
{
    public CustomItemsCollection(IEnumerable<ItemViewModel> items)
        : base(items) { }

   // your other code here
}

Then you can do this:

var filteredItems = _CustomItems.Where(c => c.Property.equals(SomeValue));
var collection = new CustomItemsCollection(filteredItems);
return collection;

Upvotes: 1

Related Questions