Cannot convert type IEnumerable to ObservableCollection…are you missing a cast?

后端 未结 2 1191
余生分开走
余生分开走 2020-12-21 10:58

I\'m trying to return entities where the bool \"isAssy\" is true:

 public ObservableCollection ParentAssemblyBOM
 {
      get {return          


        
相关标签:
2条回答
  • 2020-12-21 11:44

    ObservableCollection<T> has an overloaded constructor that accepts an IEnumerable<T> as a parameter. Assuming that your Linq statement returns a collection of MasterPartsList items:

    public ObservableCollection<MasterPartsList> ParentAssemblyBOM
    {
        get 
        {
            var enumerable = this._parentAssemblyBOM
                                 .Where(parent => parent.isAssy == true);
    
            return new ObservableCollection<MasterPartsList>(enumerable); 
        }
    }
    
    0 讨论(0)
  • 2020-12-21 11:48

    You have to explicitly create the ObservableCollection which at it's most simplest is:

    public ObservableCollection<MasterPartsList> ParentAssemblyBOM
    {
        get {return new ObservableCollection<MasterPartsList>(this._parentAssemblyBOM.Where(parent => parent.isAssy == true)); }
    }
    

    This is potentially inefficient as you are creating new collection every time. However, this might be the simplest solution if you are returning a radically different set of data each time. Otherwise you have to loop through the collection removing items that are no longer in the return set and adding new items.

    0 讨论(0)
提交回复
热议问题