Unable to case from IList<Entity> to Generic.List

Posts   
 
    
Posts: 3
Joined: 30-Jun-2008
# Posted on: 11-Jul-2008 17:59:00   

I don't understand why one of these examples works and the other doesn't

working example:

public static List<CustomerEntity> GetAllCustomers() { CustomerCollection customers = new CustomerCollection(); customers.GetMulti(null); return (List<CustomerEntity>)customers.Items; }

this example throw an casting exception on the return statement:

public static List<ProductEntity> GetProductsForCustomer(int custID) { CustomerEntity customer = new CustomerEntity(custID); OrderCollection orders = customer.Orders; ProductCollection products = new ProductCollection();

    foreach (OrderEntity order in orders)
    {
        foreach (ProductEntity product in order.ProductCollectionViaOrderProduct)
            products.Add(product);
    }
    return (List<ProductEntity>)products.Items;
}

Thanks for the help.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Jul-2008 20:34:59   

Please post LLBLGen version, RuntimeLibraries version, stack trace and exact exception message: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=7725

David Elizondo | LLBLGen Support Team
Posts: 3
Joined: 30-Jun-2008
# Posted on: 11-Jul-2008 20:58:41   

Sorry about that.

I am using LLBLGen Pro. Version 2.6

Using the following runtime libraries: \LLBLGen Pro v2.6\RuntimeLibraries\DotNet20\SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll \LLBLGen Pro v2.6\RuntimeLibraries\DotNet20\SD.LLBLGen.Pro.DQE.SqlServer.NET20.dll

Stack: System.InvalidCastException was unhandled by user code Message="Unable to cast object of type 'HeinensAPI.CollectionClasses.ProductCollection' to type 'System.Collections.Generic.List`1[HeinensAPI.EntityClasses.ProductEntity]'." Source="App_SubCode_CSCode.peiootrw" StackTrace: at ProductManager.GetProductsForCustomer(Int32 custID) in c:\Barkley-svn\Heinens\src\website\App_Code\CSCode\ProductManager.cs:line 23 at secure_home.Page_Load(Object sender, EventArgs e) in c:\Barkley-svn\Heinens\src\website\secure\home.aspx.cs:line 17 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Exception: System.InvalidCastException: Unable to cast object of type 'HeinensAPI.CollectionClasses.RewardCollection' to type 'System.Collections.Generic.List`1[HeinensAPI.EntityClasses.RewardEntity]'.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Jul-2008 06:41:45   

The direct cast fails. Try casting the interface:

public static IList<ProductEntity> GetProductsForCustomer(int custID)
    {
        CustomerEntity customer = new CustomerEntity(custID);
        OrderCollection orders = customer.Orders;
        ProductCollection products = new ProductCollection();

        foreach (OrderEntity order in orders)
        {
            foreach (ProductEntity product in order.ProductCollectionViaOrderProduct)
                products.Add(product);
        }
        return (IList<ProductEntity>)products.Items;
    }

David Elizondo | LLBLGen Support Team