开发者

Need help converting SQL statement with group by and sum into LINQ to Entities

开发者 https://www.devze.com 2023-03-14 22:05 出处:网络
I have the following SQL statement that I am trying to convert into LINQ to Entities, but the correct solution is alluding me at the moment:

I have the following SQL statement that I am trying to convert into LINQ to Entities, but the correct solution is alluding me at the moment:

select  op.orders_products_id,
        op.products_id,
        op.products_model,
        op.products_name,
        op.products_price,
        sum(op.products_quantity) as quantity,
        sum(op.count_quantity) as count_quantity
from orders_products op
where orders_id = 574214
group by op.products_id
order by op.products_model, op.orders_products_id

In this particular SQL statement, the orders_products_id is the primary key for the table, so when we group by the products_id value, it will collapse any duplicate lines in the table for the same product_id, and the quantity column result will be the total quantity for all the columns. Given the ordering statement, the op.orders_products_id should end up being the first line ID for each particular product that has multiple entries in the same table.

To select the lines using LINQ to Entities without doing the grouping, I would use the following code:

using (var uow = new StoreEntities()) {
    var query = from op in uow.OrderProducts
                where op.OrderID == 574214
                orderby op.Model, op.OrderProductID
                select new {
                    ID = op.OrderProductID,
             开发者_开发问答       ProductID = op.ProductID,
                    Model = op.Model,
                    Name = op.Name,
                    SalePrice = op.SalePrice,
                    Qty = op.Qty,
                    CountedQty = op.CountedQty,
                };
    var list = query.ToList();
}

But I am completely lost at the moment as to how I would go about doing the grouping to get the same result as my original SQL? When I do grouping in LINQ to Entities, the result of the grouping is a new sequence with a key and the details of the items for that key. How do I flatten that out, so that I can get the results out similar to how I wrote the original SQL query?


If you don't want to use an aggregation function on a non-grouped property you will need to arbitrarily selects a row, in the example I used the FirstOrDefault() method

var query = (from op in uow.OrderProducts
            where op.OrderID == 574214
            group op by op.OrderProductID
            select new 
            {
              ID = op.Key,
              ProductID = op.FirstOrDefault().ProductID,
              Model = op.FirstOrDefault().Model,
              Name = op.FirstOrDefault().Name,
              SalePrice = op.FirstOrDefault().SalePrice,
              Qty = op.Select(p => p.products_quantity).Sum(),
              CountedQty = op.Select(p => p.CountedQty).Sum(),
             })
             .OrderBy(op => op.ProductID)
             .ThenBy(op => op.OrderProductID);
0

精彩评论

暂无评论...
验证码 换一张
取 消