List<> of objects, different types, sort and pull out types individually?

Posted by Brazos on Stack Overflow See other posts from Stack Overflow or by Brazos
Published on 2011-06-27T06:03:23Z Indexed on 2011/06/27 8:22 UTC
Read the original article Hit count: 238

Filed under:
|
|
|

I've got a handful of products, any, all, or none of which may be associated with a specific submission. All 7 products are subclasses of the class Product. I need to store all the products associated with a submission, and then retrieve them and their field data on my presentation layer. I've been using a List, and List, but when I use the OfType, I throw an error saying that I can't implicitly convert systems.generic.IEnumerable to type 'Product'. I've tried to cast, but to no avail.

When I use prodlist.OfType<EPL>(); there are no errors, but when I try and store that in an instance of EPL "tempEpl", I get the aforementioned cast-related error. What gives? Code below.

 ProductService pserv = new ProductService();
    IList<object> prodlist = pserv.getProductById(x);

    EPL tempEpl = new EPL();

    if ((prodlist.OfType<EPL>()) != null)
    {            
       tempEpl = prodlist.OfType<EPL>();  // this throws a conversion error. 

    }

the Data layer

List<object> TempProdList = new List<object>();

    conn.Open();

    SqlCommand EplCmd = new SqlCommand(EPLQuery, conn);
    SqlDataReader EplRead = null;
    EplRead = EplCmd.ExecuteReader();

    EPL TempEpl = new EPL();
    if (EplRead.Read()) 
    {

        TempEpl.Entity1 = EplRead.GetString(0);
        TempEpl.Employees1 = EplRead.GetInt32(1);
        TempEpl.CA1 = EplRead.GetInt32(2);
        TempEpl.MI1 = EplRead.GetInt32(3);
        TempEpl.NY1 = EplRead.GetInt32(4);
        TempEpl.NJ1 = EplRead.GetInt32(5);
        TempEpl.PrimEx1 = EplRead.GetInt32(6);
        TempEpl.EplLim1 = EplRead.GetInt32(7);
        TempEpl.EplSir1 = EplRead.GetInt32(8);
        TempEpl.Premium1 = EplRead.GetInt32(9);
        TempEpl.Wage1 = EplRead.GetInt32(10);
        TempEpl.Sublim1 = EplRead.GetInt32(11);

        TempProdList.Add(TempEpl);
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET