C# 如何将IEnumerable转换为ObservableCollection?

C# 如何将IEnumerable转换为ObservableCollection?,c#,C#,如何根据 var myObservableCollection=新的ObservableCollection(myIEnumerable); 这将创建当前IEnumerable的浅拷贝,并将其转换为可观察集合 如果您使用的是非泛型IEnumerable,您可以这样做: public ObservableCollection<object> Convert(IEnumerable original) { return new ObservableCollection<o

如何根据

var myObservableCollection=新的ObservableCollection(myIEnumerable);
这将创建当前IEnumerable的浅拷贝,并将其转换为可观察集合

  • 如果您使用的是非泛型
    IEnumerable
    ,您可以这样做:

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

  • 为了使事情变得更简单,您可以从中创建一个方法

    public static class Extensions
    {
        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> col)
        {
            return new ObservableCollection<T>(col);
        }
    }
    
    公共静态类扩展
    {
    公共静态ObservableCollection ToObservableCollection(此IEnumerable列)
    {
    返回新的ObservableCollection(col);
    }
    }
    
    然后可以对每个IEnumerable调用该方法

    var lst = new List<object>().ToObservableCollection();
    
    var lst=new List().ToObservableCollection();
    
    ObservableCollection distinctPkgIdList=新的ObservableCollection();
    guPackgIds.Distinct().ToList().ForEach(i=>distinctPkgIdList.Add(i));
    //distinctPkgIdList-可观察收集
    //guPackgIds.Distinct()-IEnumerable
    
    将IEnumerable转换为ObservableCollection的C函数

    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;
        }
    
    private ObservableCollection IEnumeratorToObservableCollection(IEnumerable源)
    {
    ObservableCollection SourceCollection=新的ObservableCollection();
    IEnumerator enumItem=source.GetEnumerator();
    var gType=source.GetType();
    string collectionFullName=gType.FullName;
    Type[]genericTypes=gType.GetGenericArguments();
    字符串className=genericTypes[0]。名称;
    string classFullName=genericTypes[0]。FullName;
    字符串assName=(classFullName.Split('.')[0];
    //获取名称字符串中包含的类型
    Type Type=Type.GetType(classFullName,true);
    //创建该类型的实例
    对象实例=Activator.CreateInstance(类型);
    List-oProperty=instance.GetType().GetProperties().ToList();
    while(enumItem.MoveNext())
    {
    Object instanceInner=Activator.CreateInstance(类型);
    var x=枚举项。当前;
    foreach(oProperty中的var项目)
    {
    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);
    }
    返回源集合;
    }
    
    这比使用ForEach循环将项目逐个插入集合快吗?@Rexxo使用构造函数将稍微快一点。在内部将项目复制到内部集合,但是如果您执行foreach并调用
    Add
    ,这会产生很多额外的内容,而这些内容在最初填充时是不必要的,这会导致它稍微变慢。对于一个很酷的问题,简单且最好的答案很酷的答案,我将其添加到固定答案中。
    ObservableCollection<decimal> distinctPkgIdList = new ObservableCollection<decimal>();
    guPackgIds.Distinct().ToList().ForEach(i => distinctPkgIdList.Add(i));
    
    // distinctPkgIdList - ObservableCollection
    // guPackgIds.Distinct() - IEnumerable 
    
    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;
        }