0
如何通過wl.WishlistID對此查詢的結果進行分組?linq顯示在視圖中的結果 - 如何應用分組
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
但我似乎不能夠通過使用克至訪問任何屬性將[PropertyName]
這種分組達到或多或少我想要什麼,但我想將結果轉換爲新的或匿名的類型,而不是返回整個對象。
group wl by wl.WishlistID into g
g
是IGrouping<typeof(wl.WishlistID),typeof(wl)>
型的,這基本上是所有wl
的的使用相同的密鑰wl.WishlistID
集合:
var results = context.WishlistItems.GroupBy(x => x.Wishlist).
Select(group => new { wl = group.Key, items = group.ToList() }).ToList();