我有这个课:

public class ItemsForMonthYear
{
    public String ItemDescription { get; set; }
    public String monthYr { get; set; }
    public int TotalPackages { get; set; }
    public Decimal TotalPurchases { get; set; }
    public Decimal AveragePrice { get; set; }
    public Double PercentOfTotal { get; set; }
}


...将数据存储到其中我有用于存储N个的通用列表:

List<ItemsForMonthYear> itemsForMonthYearList;


...然后我稍后从该列表中读取以填充电子表格,这非常简单:

totPackagesCell.Value2 = ifmy.TotalPackages;
totPurchasesCell.Value2 = ifmy.TotalPurchases;
avgPriceCell.Value2 = ifmy.AveragePrice;


...但是下一个所需的单元格val是基于给定monthYr的所有TotalPurchases值计算得出的。因此,我需要为具有相同monthYr值的所有ItemsForMonthYear“记录”计算“总百分比”值。在SQL中,我可以做这样的事情:

SELECT SUM(TotalPurchases) FROM ItemsForMonthYear WHERE monthYr = 'Apr 14'


...然后将结果除以每个ItemDescription的TotalPurchases值

IOW,如果“ Apr 14”月份的所有TotalPurchases的总和为一百万美元,而ItemDescription“可可粉扑”的TotalPurchases为十万美元,我可以计算出“可可粉扑”的PercentOfTotal为10% 。

我可以“强行使用它”并遍历整个通用列表:

Decimal allTotalPurchases;
foreach (ItemsForMonthYear ifmy in itemsForMonthYearList)
{
    if (ifmy.monthYr.Equals("Apr 14")
    {
        allTotalPurchases = allTotalPurchases + ifmy.TotalPurchases;
    }
}


...但是我认为使用LINQ可以实现更优雅/更友好的性能。我想做这样的事情:

percentOfTotalCell.Value2 = GetPercentForPurchaseForMonthYearAsStr(ifmy.TotalPurchases, curMonth);

private String GetPercentForPurchaseForMonthYearAsStr(decimal totPurchasesForItemMonthYear, string totPurchasesForMonthYear)
{
    percentOfTotal = // What LINQ magic goes here?
    percentOfTotalStr = percentOfTotal.ToString("P", CultureInfo.InvariantCulture);
    return percentOfTotalStr;
}


...其中arg“ totPurchasesForItemMonthYear”的值应为上述示例中的十万美元,而arg“ totPurchasesForMonthYear”的值应为“ Apr 14”。

有人知道这个聪明的LINQ技巧吗?

最佳答案

下一个所需的单元格值是基于给定monthYr的所有TotalPurchases值计算得出的值。因此,我需要为具有相同monthYr值的所有ItemsForMonthYear“记录”计算“总百分比”值。


好吧,首先,您需要获得具有相同monthYr的那些:

var allMonthYr = itemsForMonthYearList.Where(x => x.monthYr == ifmy.monthYr);


然后是一个简单的Sum

var totalPurchasesOfMonthYr = allMonthYr.Sum(x => x.TotalPurchases);
var percentOfTotal = ifmy.TotalPurchases / totalPurchasesOfMonthYr;


现在,事实证明这是非常低效的-因为您不必要地迭代此列表很多次。最好分组然后求和一次:

var groupedItemsForMonthYr = itemsForMonthYearList.ToLookup(x => x.monthYr);
var totalsForMonthYr = groupedItemsForMonthYr.ToDictionary(
    x => x.Key,
    x => x.Sum(x => x.TotalPurchases)
);

var percentOfTotal = ifmy.TotalPurchases / totalsForMonthYr[ifmy.monthYr];

08-05 04:48