How to convert IEnumerable to ObservableCollection?

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

How to convert IEnumerable to ObservableCollection?

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 19:59

    To make things even more simple you can create an Extension method out of it.

    public static class Extensions
    {
        public static ObservableCollection ToObservableCollection(this IEnumerable col)
        {
            return new ObservableCollection(col);
        }
    }
    

    Then you can call the method on every IEnumerable

    var lst = new List().ToObservableCollection();
    
        

    提交回复
    热议问题