如何按 wl.WishlistID 对该查询的结果进行分组?
var projected = (from wl in context.Wishlists.Where(x => x.UserId == 6013)
from wli in wl.WishlistItems
select new wishListProjection
{
wlId = wl.WishlistID,
wlUserId = (int)wl.UserId,
wlDesc = wl.description,
wlTitle = wl.Title,
wliWishlistItemID = wli.WishlistItemID,
wliQtyWanted = (int)wli.QtyWanted,
wliPriceWhenAdded = wli.PriceWhenAdded,
wliDateAdded = (DateTime)wli.DateAdded,
wliQtyBought = (int)wli.QtyBought,
}).ToList();
这将返回我想要的结果,但我希望能够在视图中迭代它们而不重复父级 Wishlist 对象。我试过添加这一行:
group wl by wl.WishlistID into g
但我似乎无法使用 g.[propertyname] 访问任何属性
这种分组或多或少地实现了我想要的,但我想将结果转换为新的或匿名类型,而不是返回整个对象。
var results = context.WishlistItems.GroupBy(x => x.Wishlist).
Select(group => new { wl = group.Key, items = group.ToList() }).ToList();