简体   繁体   中英

How to get SUM of column using Entity Framework LINQ

I want to get SUM of column "Weight" using LINQ and store in list. Following is code of join operation of two columns.

var que = (from o in db.OrderPlans.Where(i => i.DemandId == _demandId)
join f in db.Fabrics
on o.FabricId equals f.Id
select new
{
    f.Id,
    f.Name,
    o.Width,
    f.Type,
    o.GSM,
    o.Weight
}).ToList();

assuming the code you posted above works and let's pretend you stored that in a variable called joinedList (and that you want the sum of Weight across groups formed by the distinct combinations of all the other fields):

var groupedList = joinedList.GroupBy(grp => new {
                             grp.Id,
                             grp.Name,
                             grp.Width,
                             grp.Type,
                             grp.GSM
}).Select(grp => new {
                             grp.Key.Id,
                             grp.Key.Name,
                             grp.Key.Width,
                             grp.Key.Type,
                             grp.Key.GSM,
                             SumOfWeight = grp.Sum(w => w.Weight)
}).ToList();

在您的代码末尾附加此.Groupby(w => w.Weight).Convert< int >(g=> g.Sum());

It seems to me that you have a one-to-many relation between Fabric and OrderPlan : every Fabric has zero or more OrderPlans , every OrderPlan belongs to exactly one Fabric , namely the Fabric that the foreign key refers to.

It seems to me that you want several Fabric properties with (several properties of) their OrderPlans and the total Weight of these OrderPlans .

Whenever you have a one-to-many relation or many-to-many relation and you want to fetch "items with their sub-items", like Schools with their Students, or Customers with their Orders, or Orders with their OrderLines, you use Queryable.GroupJoin instead of a standard join.

// GroupJoin Fabrics with some of their OrderPlans:
var result = dbContext.Fabrics
    .GroupJoin(dbContext.OrderPlans.Where(orderPlan => ...)
    fabric => fabric.Id,              // from each Fabric take the primary key
    orderPlan => orderPlan.FabricId,  // from each OrderPlan take the foreign key

    // ResultSelector: take every Fabric with all his matching OrderPlans to make one new object:
    (fabric, orderPlansOfthisFabric) => new
    {
        // Select the Fabric properties you want
        Id = fabric.Id,
        Name = fabric.Name,
        Type = fabric.Type,

        OrderPlans = orderPlansOfThisFabric.Select(orderPlan => new
        {
             // Select the OrderPlan properties you want:
             Width = orderPlan.Width,
             Gsm = orderPlan.Gsm,
        }

        // Weight: calculate the sum of all OrderPlans of this Fabric:
        Weight = orderPlansOfThisFabric
                 .Select(orderPlan => orderPlan.Weight)
                 .Sum(),
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM