How to convert IEnumerable to ObservableCollection?

前端 未结 5 1936
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 19:34

How to convert IEnumerable to ObservableCollection?

5条回答
  •  遥遥无期
    2020-11-29 20:18

    1. If you're working with non-generic IEnumerable you can do it this way:

      public ObservableCollection Convert(IEnumerable original)
      {
          return new ObservableCollection(original.Cast());
      }
      
      
    2. If you're working with generic IEnumerable you can do it this way:

      public ObservableCollection Convert(IEnumerable original)
      {
          return new ObservableCollection(original);
      }
      
    3. If you're working with non-generic IEnumerable but know the type of elements, you can do it this way:

      public ObservableCollection Convert(IEnumerable original)
      {
          return new ObservableCollection(original.Cast());
      }
      
    4. 提交回复
      热议问题