Multiple SUM using LINQ

后端 未结 5 2813
无人共我
无人共我 2021-02-20 18:14

I have a loop like the following, can I do the same using multiple SUM?

foreach (var detail in ArticleLedgerEntries.Where(pd => pd.LedgerEntryType == LedgerEn         


        
5条回答
  •  旧时难觅i
    2021-02-20 18:45

    You can call Sum three times, but it will be slower because it will make three loops.

    For example:

    var list = ArticleLedgerEntries.Where(pd => pd.LedgerEntryType == LedgerEntryTypeTypes.Unload
                                       && pd.InventoryType == InventoryTypes.Finished))
    
    var totalWeight = list.Sum(pd => pd.GrossWeight);
    var totalLength = list.Sum(pd => pd.Length);
    var items = list.Sum(pd => pd.NrDistaff); 
    

    Because of delayed execution, it will also re-evaluate the Where call every time, although that's not such an issue in your case. This could be avoided by calling ToArray, but that will cause an array allocation. (And it would still run three loops)

    However, unless you have a very large number of entries or are running this code in a tight loop, you don't need to worry about performance.


    EDIT: If you really want to use LINQ, you could misuse Aggregate, like this:

    int totalWeight, totalLength, items;
    
    list.Aggregate((a, b) => { 
        weight += detail.GrossWeight;
        length += detail.Length;
        items  += detail.NrDistaff;
        return a;
    });
    

    This is phenomenally ugly code, but should perform almost as well as a straight loop.

    You could also sum in the accumulator, (see example below), but this would allocate a temporary object for every item in your list, which is a dumb idea. (Anonymous types are immutable)

    var totals = list.Aggregate(
        new { Weight = 0, Length = 0, Items = 0},
        (t, pd) => new { 
            Weight = t.Weight + pd.GrossWeight,
            Length = t.Length + pd.Length,
            Items = t.Items + pd.NrDistaff
        }
    );
    

提交回复
热议问题