I’m receiving this error in my Linq statement —
Cannot implicitly convert type ‘System.Collections.Generic.IEnumerable’ to ‘hcgames.ObjectClasses.ShoppingCart.ShoppingCartCartAddon’. An explicit conversion exists (are you missing a cast?)
From this query
ShoppingCartItems items = Cart.GetAllItems();
ShoppingCartCartAddons addons = Cart.GetAllAddons();
var stuff = from x in items
select new ShoppingCartItem()
{
ProductID = x.ProductID,
Quantity = x.Quantity,
Name = x.Name,
Price = x.Price,
Weight = x.Weight,
Addons = (from y in addons
where y.ShoppingCartItemID == x.ID
select y)
};
I can not figure out how to cast this properly. Any suggestions?
Thanks for your help!
Considering the code you posted, there’s at least one ways you can resolve it. The simpler, less elegant is to modify your
ShoppingCartItem.Addonssignature like the following, since your ShoppingCartCartAddons collection does not have any other functionalities :Explanation : you are basicly trying to initialize a
Collection<ShoppingCartCartAddon>implementation,ShoppingCartCartAddonsfrom anIEnumerable<ShoppingCartCartAddon>, thus compiler goes wonkers.Otherwise, you can define a contructor for
ShoppingCartCartAddonswhich takes in anIEnumerable<ShoppingCartCartAddon>to intialize itself.