我有兩個類ShoppingCart
和CartItems
這樣的:如何返回嵌套列表中的實體框架
public class ShoppingCart
{
public Guid Id { get; set; }
public DateTime CreatedOn { get; set; }
public Guid OwnerId { get; set; }
public ICollection<CartItem> Items { get; set; }
}
public class CartItem
{
public Guid Id { get; set; }}
public int Quantity { get; set; }
public Guid ProductId { get; set; }
public Guid ShoppingCartId { get; set; }
}
我想用這種方法,通過ownerId
得到所有CartItems
:
public IEnumerable<CartItem> GetCartItems(Guid ownerId)
{
return _shoppingCarts.Where(row => row.OwnerId == ownerId)
.Select(row => row.Items)
.ToList() ;
}
但它返回一個錯誤:
Cannot implicitly convert type System.Collections.Generic.List<System.Collections.Generic.ICollection<CartItem>>'to System.Collections.Generic.IEnumerable<CartItem>
我認爲你的答案更好,謝謝 – temp125050