Cast LINQ result to ObservableCollection

后端 未结 6 1188
死守一世寂寞
死守一世寂寞 2020-11-29 23:55

The fact that it is a LINQ result might perhaps not be relevant for the question, but I\'m mentioning it anyway - since this is the context which has resulted in this questi

6条回答
  •  [愿得一人]
    2020-11-30 00:18

    Just use:

    ObservableCollection x = new ObservableCollection(enumerable);
    

    That will do the required copying. There's no way of observing changes to the live query - although the idea of an ObservableQuery is an interesting (though challenging) one.

    If you want an extension method to do this, it's simple:

    public static ObservableCollection ToObservableCollection
        (this IEnumerable source)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }
        return new ObservableCollection(source);
    }
    

提交回复
热议问题