Linq多次加入,然后GroupBy和OrderBy,尝试了我能找到的一切

时间:2016-07-14 09:48:03

标签: linq

var CartItemsGroup = (from Cart in db.BuyOnlineCartMasters // orderby this Cart.CartID descending 
    join CartDetails in db.BuyOnlineCartDetails 
        on Cart.CartID equals CartDetails.CartID

    join ProductMaster in db.tblBuyOnlineMasters.Where(x => x.CompanyID == CompanyID)
        on CartDetails.BuyOnlineID equals ProductMaster.BuyOnlineID

//I tried here did not work ,  orderby Cart.CartID descending

    select new { Cart.CartID, Cart.InvoiceNumber, ProductMaster.CompanyID,
    ProductMaster.Price, ProductMaster.Title, ProductMaster.OfferPrice, 
    CartDetails.SoldPrice, CartDetails.Qty, CartDetails.ShippingStatus, 
    CartDetails.DeliveryStatus, TotalAmt = (CartDetails.Qty * CartDetails.SoldPrice)}

    into x

//I tried here did not work, orderby x.CartID descending

//TODO: where conditions are not set for cart like payment paid etc
    group x by x.CartID)
    .ToList();

我想通过cartID订购结果,因为groupby子句我无法完成它

Screenshot of the output structure

2 个答案:

答案 0 :(得分:1)

在分组后,订购需要

要实现目标,请替换

group x by x.CartID

group x by x.CartID into g
orderby g.Key descending
select g

答案 1 :(得分:1)

首先选择您的数据然后分组。那可行。我的表格样本

from data in
    (from o in DB.GM_ORDER
        join g in DB.GM_ORDERITEMS on o.ORDERID equals g.ORDERID
        join i in DB.GM_ITEM on g.ITEMID equals i.ITEMID
    where o.ORDERID<=11160 /* or any other filter to be applied */
    orderby o.ORDERID descending, i.ITEMCODE ascending /* in your case ORDERID will be CARTID. Continue sorting within the ORDER/CART with a second parameter (in your case can be delivery status) */
    select new { o.ORDERID, i.ITEMCODE, g.ITEMID, Total = (g.CURR_PRICE * g.QTY) }
) select data
into x
group x by x.ORDERID