KentZhou
KentZhou

Reputation: 25573

How to convert IEnumerable to ObservableCollection?

How to convert IEnumerable to ObservableCollection?

Upvotes: 169

Views: 111607

Answers (5)

Elisha
Elisha

Reputation: 23780

  1. If you're working with non-generic IEnumerable you can do it this way:

    public ObservableCollection<object> Convert(IEnumerable original)
    {
        return new ObservableCollection<object>(original.Cast<object>());
    }
    
  2. If you're working with generic IEnumerable<T> you can do it this way:

    public ObservableCollection<T> Convert<T>(IEnumerable<T> original)
    {
        return new ObservableCollection<T>(original);
    }
    
  3. If you're working with non-generic IEnumerable but know the type of elements, you can do it this way:

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

Upvotes: 85

B.Balamanigandan
B.Balamanigandan

Reputation: 4875

The C# Function to Convert the IEnumerable to 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;
    }

Upvotes: 1

Sudharshan
Sudharshan

Reputation: 348

ObservableCollection<decimal> distinctPkgIdList = new ObservableCollection<decimal>();
guPackgIds.Distinct().ToList().ForEach(i => distinctPkgIdList.Add(i));

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

Upvotes: 2

Scott Chamberlain
Scott Chamberlain

Reputation: 127573

As per the MSDN

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

This will make a shallow copy of the current IEnumerable and turn it in to a ObservableCollection.

Upvotes: 260

markwilde
markwilde

Reputation: 1997

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

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

Then you can call the method on every IEnumerable

var lst = new List<object>().ToObservableCollection();

Upvotes: 41

Related Questions