开发者

Cannot implicitly convert type System.Collection.Generic.IEnumberable

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()
        开发者_JAVA技巧            {
                        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!


I'm shooting in the dark since I don't know what type ShoppingCartItem.Addons is, but judging from the error, I would say it's expecting that type to be hcgames.ObjectClasses.ShoppingCart.ShoppingCartCartAddon

Your LINQ query is turning an IEnumerable<ShoppingCartCartAddon>. You could adding .FirstOrDefault() to the LINQ Query to see if that clears things up.


Considering the code you posted, there's at least one ways you can resolve it. The simpler, less elegant is to modify your ShoppingCartItem.Addons signature like the following, since your ShoppingCartCartAddons collection does not have any other functionalities :

namespace hcgames.ObjectClasses.ShoppingCart 
{ 
    [Serializable] 
    public class ShoppingCartItem 
    {
        public ShoppingCartItem(); 
        public ShoppingCartItem(DataRow dr); 
        public IEnumerable<ShoppingCartCartAddon> Addons { get; set; } 
        public string CartID { get; set; } 
        public int ID { get; set; } 
        public string Image { get; set; } 
        public string Name { get; set; } 
        public string Price { get; set; } 
        public long ProductID { get; set; } 
        public int Quantity { get; set; } 
        public decimal Weight { get; set; } 
    }
}

Explanation : you are basicly trying to initialize a Collection<ShoppingCartCartAddon> implementation, ShoppingCartCartAddons from an IEnumerable<ShoppingCartCartAddon>, thus compiler goes wonkers.

Otherwise, you can define a contructor for ShoppingCartCartAddons which takes in an IEnumerable<ShoppingCartCartAddon> to intialize itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜