在WPF+EDMX的工作中经常从Server端返回数组xxx[],但是客户端需要的类型是ObservableCollection,如何快速转化数组成为我们所需要的? 下面我做了一个简单的实现,希望对大家有所帮助。如果你需要把ObservableCollection转化成List就好办了。首先using System.Linq;之后可以直接调用ToArray。
public static ObservableCollection<T> ConvertList2ObserveableCollection<T>(T[] temp) { ObservableCollection<T> scheduleInProcessOwner = new ObservableCollection<T>(); List<T> tempList = new List<T>(); if (temp != null && temp.Count() > 0) { tempList = temp.ToList(); } tempList.ForEach(p => scheduleInProcessOwner.Add(p)); return scheduleInProcessOwner; }
List<string> names = new List<string>() { "Dave", "Cathy", "Jane","Dog" };
ObservableCollection<string> observableCollection = new ObservableCollection<string>();
names.ForEach(x => observableCollection .Add(x));
ObservableCollection<string> observableCollection = new ObservableCollection<string>();
names.ForEach(x => observableCollection .Add(x));