Return a List<T> or a ObservableCollection<T>

    It's crazy generic method time! Here is a method I created for fun that either returns a List<T> or a ObservableCollection<T>, depending on what you pass in via generic parameters. It creates a list of Product entity instances with some fake data.

    The L stands for the type of ICollection you want to create and return. For example a List<T> or a ObservableColleciton<T>. The P stands for a Product class type. This could be replaced with any type of entity, just replace the condition of where : Product with the entity type of your choice.

    You can call it using this code:

    List<Product>  productList = CreateProductList<List<Product>, Product>();
     
    or
     

    ObservableCollection<Product> productOC =

    CreateProductList<ObservableCollection<Product>, Product>();

     

    Its just for fun ... so enjoy.

       1:  private L CreateProductList<L, P>()
       2:      where P : Product, new()
       3:      where L : ICollection<P>, new()
       4:  {
       5:      L products = new L
       6:  {
       7:      new P
       8:          {
       9:              ProductId = 70,
      10:              ProductName = "Outback Lager",
      11:              QuantityPerUnit = "24 - 355 ml bottles",
      12:              UnitsInStock = 15,
      13:              UnitPrice = 15,
      14:              UnitsOnOrder = 10,
      15:              ReorderLevel = 30,
      16:              Discontinued = false
      17:          },
      18:      new P
      19:          {
      20:              ProductId = 71,
      21:              ProductName = "Flotemysost",
      22:              QuantityPerUnit = "10 - 500 g pkgs.",
      23:              UnitsInStock = 25,
      24:              UnitPrice = (decimal)21.5,
      25:              UnitsOnOrder = 0,
      26:              ReorderLevel = 0,
      27:              Discontinued = false
      28:          },
      29:      new P
      30:          {
      31:              ProductId = 35,
      32:              ProductName = "Steeleye Stout",
      33:              QuantityPerUnit = "24 - 12 oz bottles",
      34:              UnitsInStock = 20,
      35:              UnitPrice = (decimal)18,
      36:              UnitsOnOrder = 0,
      37:              ReorderLevel = 15,
      38:              Discontinued = false
      39:          },
      40:      new P
      41:          {
      42:              ProductId = 53,
      43:              ProductName = "Perth Pasties",
      44:              QuantityPerUnit = "48 pieces",
      45:              UnitsInStock = 0,
      46:              UnitPrice = (decimal)32.80,
      47:              UnitsOnOrder = 0,
      48:              ReorderLevel = 0,
      49:              Discontinued = true,
      50:              DiscontinuedDate = new DateTime(1996, 7, 4)
      51:          }
      52:  };
      53:      return products;
      54:  }
    #1 » Return a List or a ObservableCollection on 7.22.2008 at 5:05 PM

    Pingback from » Return a List or a ObservableCollection



    #2 Dew Drop - July 23, 2008 | Alvin Ashcraft's Morning Dew on 7.23.2008 at 9:39 AM

    Pingback from Dew Drop - July 23, 2008 | Alvin Ashcraft's Morning Dew



    #3 Braulio on 10.18.2008 at 7:39 AM

    Interesting, I have been digging into Silverlight and haven't found a "new" constructor on Observable collection that accepts as param a generic List, that's a pain in the neck if you are using LINQ and generate an embedded list.

    Using your apporach should be easy to have a generic cast function, no more need for nasty loops over there :-).



    Leave a Comment