2010-03-08 65 views
0

我收到這個錯誤在我的LINQ的聲明---無法隱式轉換類型System.Collection.Generic.IEnumberable

無法隱式轉換類型「System.Collections.Generic.IEnumerable」到「hcgames.ObjectClasses。 ShoppingCart.ShoppingCartCartAddon」。一個顯式轉換存在(是否缺少強制轉換?)

從這個查詢

 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) 
        }; 

我無法弄清楚如何正確施放此。有什麼建議麼?

感謝您的幫助!

+0

你可以發佈'ShoppingCartCartAddons'和'ShoppingCartItems.Addons'簽名嗎? – 2010-03-08 13:54:44

+0

namespace hcgames.ObjectClasses.ShoppingCart { [Serializable] public class ShoppingCartItem { public ShoppingCartItem(); public ShoppingCartItem(DataRow dr); public ShoppingCartCartAddons Addons {get;組; } public string CartID {get;組; } public int ID {get;組; } public string Image {get;組; } public string Name {get;組; } public string Price {get;組; } public long ProductID {get;組; } public int Quantity {get;組; } public decimal Weight {get;組; } } } – TheGeekYouNeed 2010-03-08 15:13:47

+0

using System; using System.Collections.ObjectModel; 命名空間hcgames.ObjectClasses.ShoppingCart { [序列化] 公共類ShoppingCartCartAddons:收集 { 公共ShoppingCartCartAddons(); } } – TheGeekYouNeed 2010-03-08 15:14:57

回答

0

考慮到您發佈的代碼,至少有一種方法可以解決它。該更簡單,更優雅是修改ShoppingCartItem.Addons簽名像下面這樣,因爲你的ShoppingCartCartAddons收藏沒有任何其他功能:

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; } 
    } 
} 

說明:您basicly嘗試從IEnumerable<ShoppingCartCartAddon>初始化Collection<ShoppingCartCartAddon>實施,ShoppingCartCartAddons,因此編譯器會成爲執行者。

否則,您可以爲ShoppingCartCartAddons定義一個構造函數,它需要一個IEnumerable<ShoppingCartCartAddon>來初始化它自己。

+0

我確定它爲ShoppingCartCartAddons 。現在發佈代碼。 – TheGeekYouNeed 2010-03-08 15:11:22

1

我拍攝在黑暗中,因爲我不知道ShoppingCartItem.Addons是什麼類型的,而是從錯誤來看,我會說這是預計該類型是hcgames.ObjectClasses.ShoppingCart.ShoppingCartCartAddon

你的LINQ查詢翻動IEnumerable<ShoppingCartCartAddon>。你可以在LINQ查詢中加入.FirstOrDefault()來查看是否清除了事情。

相關問題