Cách chuyển đổi IEnumerable
thành ObservableCollection
?Làm thế nào để chuyển đổi IEnumerable thành ObservableCollection?
94
A
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.
57
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>()); }
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); }
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
- 1. Làm cách nào để chuyển đổi danh sách thành ObservableCollection?
- 2. làm thế nào để chuyển đổi IEnumerable sang IObservable?
- 3. Làm thế nào để chuyển đổi một IEnumerable <IEnumerable <T>> thành IEnumerable <T>
- 4. Chuyển đổi/Truyền IEnumerable thành IEnumerable <T>
- 5. Làm thế nào để bạn chuyển đổi IEnumerable <int> thành Int32
- 6. Chuyển đổi thành IEnumerable <dynamic>?
- 7. C# - chuyển đổi loại vô danh thành observablecollection
- 8. Làm cách nào để chuyển đổi IEnumerable <Enum> thành Enum trong C#?
- 9. Chuyển đổi IEnumerable <int> thành int []
- 10. Tối ưu hóa IEnumerable thành chuyển đổi HashSet trong LINQ
- 11. Chuyển đổi IEnumerable sang DataTable
- 12. Chuyển đổi từ IEnumerable sang Danh sách
- 13. Làm thế nào để chuyển đổi một chuỗi thành CharSequence?
- 14. Làm thế nào để chuyển đổi Integer thành int?
- 15. Làm thế nào để bạn chuyển đổi SYS_GUID() thành varchar?
- 16. Làm thế nào để chuyển đổi IDN thành ASCII?
- 17. Làm thế nào để chuyển đổi/cast dài thành String?
- 18. Làm thế nào để chuyển đổi chuỗi thành mảng byte?
- 19. Làm thế nào để chuyển đổi NSData thành NSString?
- 20. Làm thế nào để chuyển đổi sqldatetime.minvalue thành datetime?
- 21. Làm thế nào để chuyển đổi byte [] thành InputStream?
- 22. Làm thế nào để chuyển đổi char thành chuỗi?
- 23. Làm thế nào để chuyển đổi SimpleXMLObject thành PHP Array?
- 24. Làm thế nào để chuyển đổi log4j.xml thành logback.xml?
- 25. Làm thế nào để chuyển đổi 'System.Windows.Media.Imaging.BitmapImage' thành 'System.Drawing.Image'?
- 26. Làm thế nào để chuyển đổi std :: string thành NSString?
- 27. Làm thế nào để chuyển đổi JSON thành chuỗi?
- 28. Làm thế nào để chuyển đổi NSString thành chuỗi C?
- 29. Làm thế nào để chuyển đổi từ pandas.DatetimeIndex thành numpy.datetime64?
- 30. làm thế nào để chuyển đổi datetime thành ngày ngắn?
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
@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. –