How to Convert List to ObservableCollection?

32,057

Solution 1

Your Products class shouldn't inherit anything.

public class Products

Accessing all the items in your collection is done through the DataCollection property of the Product class. For example,

Products myProducts = new Products();
ObservableCollection<Product> myData = myProducts.DataCollection;

It also depends on how you want to use Products. You may be able to totally do away with this class and then do something like:

ObservableCollection<Product> Products = new ObservableCollection<Product>();
Products.Add(new Product("Haystack Code Generator for .NET", 799, IMG_PATH + "Haystack.jpg"));
// etc...

Solution 2

There is a constructor in ObservableCollection that does this:

ObservableCollection<T> oc = new ObservableCollection<T>(List<T> list);

Solution 3

You can make a extension method that will let you convert any type of List into an ObservableCollection easily.

public static class CollectionUtils
{
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> thisCollection)
    {
        if (thisCollection == null) return null;
        var oc = new ObservableCollection<T>();

        foreach (var item in thisCollection)
        {
            oc.Add(item);
        }

        return oc;
    }
}

For example:

Products p = new Products();
//add your products

var collection = p.ToObservableCollection(); //use the extension method.

Solution 4

The class Products is a list. In BuildCollection you access this with the Add method. You don't need an auxiliary structure in my opinion.

To convert list to ObservableCollection use

this.Select<Product, ObservableCollection>(p  => p) 

method on collection.

Share:
32,057

Related videos on Youtube

Albert.Qing
Author by

Albert.Qing

Updated on July 09, 2022

Comments

  • Albert.Qing
    Albert.Qing almost 2 years

    I am a java Developer,new to C# silverlight. in this class I want to Convert the Products(List) to ObservableCollection.

        using System;
        using System.Collections.Generic;
        using System.Collections.ObjectModel;
        using System.Linq;
    
    namespace WPListBoxImage
    {
    /**It seems not work,if I just change List<Product> to ObservableCollection<Product>
      public class Products : List<Product>
      {
        public Products()
        {
          BuildCollection();
    
        }
    
        private const string IMG_PATH = "../Images/";
    
        public ObservableCollection<Product> DataCollection { get; set; }
    
        public ObservableCollection<Product> BuildCollection()
        {
          DataCollection = new ObservableCollection<Product>();
    
          DataCollection.Add(new Product("Haystack Code Generator for .NET", 799, IMG_PATH + "Haystack.jpg"));
          DataCollection.Add(new Product("Fundamentals of N-Tier eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundNTier_100.jpg"));
          DataCollection.Add(new Product("Fundamentals of ASP.NET Security eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundSecurity_100.jpg"));
          DataCollection.Add(new Product("Fundamentals of SQL Server eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundSQL_100.jpg"));
          DataCollection.Add(new Product("Fundamentals of VB.NET eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundVBNet_100.jpg"));
          DataCollection.Add(new Product("Fundamentals of .NET eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundDotNet_100.jpg"));
          DataCollection.Add(new Product("Architecting ASP.NET eBook", Convert.ToDecimal(19.95), IMG_PATH + "ArchASPNET_100.jpg"));
          DataCollection.Add(new Product("PDSA .NET Productivity Framework", Convert.ToDecimal(2500), IMG_PATH + "framework.jpg"));
    
          return DataCollection;
        }
      }
    }
    

    what should I do to fix it? or need to create a new class?

  • Albert.Qing
    Albert.Qing over 12 years
    You're genius! Indeed,I've been confused for this question yestoday.but can't find solution until now.Do you mind go to this link,and Paste this answer for someone had some problem ,I'll mark the answer.
  • William Melani
    William Melani over 12 years
    just noticed this wasn't actually returning an ObservableCollection. updated