2010-08-24 22 views

Trả lời

154

Theo MSDN

var myObservableCollection = new ObservableCollection<YourType>(myIEnumerable); 

này sẽ tạo một bản sao cạn của IEnumerable hiện và biến nó vào một ObservableCollection.

+0

là nhanh hơn này vì sử dụng một vòng lặp foreach để chèn từng mục vào bộ sưu tập? – Rexxo

+2

@Rexxo nó sẽ nhanh hơn một chút để thực hiện nó với hàm tạo. Bên trong [nó đang sử dụng 'foreach'] (http://referencesource.microsoft.com/System/compmod/system/collections/objectmodel/observablecollection.cs.html#cfaa9abd8b214ecb) để sao chép các mục vào bộ sưu tập nội bộ, tuy nhiên nếu bạn làm foreach và gọi 'Add' [nó sẽ được thông qua' InsertItem'] (http://referencesource.microsoft.com/System/compmod/system/collections/objectmodel/observablecollection.cs.html#767bf62bf4a3a7c1) rất nhiều thứ bổ sung không quan trọng khi ban đầu làm đầy khiến nó chậm hơn một chút. –

57
  1. Nếu bạn đang làm việc với những người không chung IEnumerable bạn có thể làm theo cách này:

    public ObservableCollection<object> Convert(IEnumerable original) 
    { 
        return new ObservableCollection<object>(original.Cast<object>()); 
    } 
    
  2. Nếu bạn đang làm việc với generic IEnumerable<T> bạn có thể làm theo cách này:

    public ObservableCollection<T> Convert<T>(IEnumerable<T> original) 
    { 
        return new ObservableCollection<T>(original); 
    } 
    
  3. Nếu bạn đang làm việc với không chung chung IEnumerable nhưng biết loại yếu tố, bạn có thể làm theo cách này:

    public ObservableCollection<T> Convert<T>(IEnumerable original) 
    { 
        return new ObservableCollection<T>(original.Cast<T>()); 
    } 
    
24

Để thực hiện những điều đơn giản hơn bạn có thể tạo một phương pháp Extension ra khỏi nó.

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

Sau đó, bạn có thể gọi phương thức trên tất cả các IEnumerable

var lst = new List<object>().ToObservableCollection(); 
3
ObservableCollection<decimal> distinctPkgIdList = new ObservableCollection<decimal>(); 
guPackgIds.Distinct().ToList().ForEach(i => distinctPkgIdList.Add(i)); 

// distinctPkgIdList - ObservableCollection 
// guPackgIds.Distinct() - IEnumerable 
1

C# Chức năng Chuyển đổi các IEnumerable để ObservableCollection

private ObservableCollection<dynamic> IEnumeratorToObservableCollection(IEnumerable source) 
    { 

     ObservableCollection<dynamic> SourceCollection = new ObservableCollection<dynamic>(); 

     IEnumerator enumItem = source.GetEnumerator(); 
     var gType = source.GetType(); 
     string collectionFullName = gType.FullName; 
     Type[] genericTypes = gType.GetGenericArguments(); 
     string className = genericTypes[0].Name; 
     string classFullName = genericTypes[0].FullName; 
     string assName = (classFullName.Split('.'))[0]; 

     // Get the type contained in the name string 
     Type type = Type.GetType(classFullName, true); 

     // create an instance of that type 
     object instance = Activator.CreateInstance(type); 
     List<PropertyInfo> oProperty = instance.GetType().GetProperties().ToList(); 
     while (enumItem.MoveNext()) 
     { 

      Object instanceInner = Activator.CreateInstance(type); 
      var x = enumItem.Current; 

      foreach (var item in oProperty) 
      { 
       if (x.GetType().GetProperty(item.Name) != null) 
       { 
        var propertyValue = x.GetType().GetProperty(item.Name).GetValue(x, null); 
        if (propertyValue != null) 
        { 
         PropertyInfo prop = type.GetProperty(item.Name); 
         prop.SetValue(instanceInner, propertyValue, null); 
        } 
       } 
      } 

      SourceCollection.Add(instanceInner); 
     } 

     return SourceCollection; 
    } 
Các vấn đề liên quan