2

我正在尝试将特定配方(所有成分)的所有数量求和为单个值以获得总数量

假设我有以下数据集:

RecipeName IngredientName ReceiptWeight
Food1      Ingredient1    5
Food1      Ingredient2    2
Food2      Ingredient1    12
Food2      Ingredient3    1

我希望得到以下信息:

RecipeName ReceiptWeight
Food1      7
Food2      13

我到目前为止的代码是:

            Grouping =
                (
                from data in dataset
                group data by data.RecipeName into recipeGroup
                let fullIngredientGroups = recipeGroup.GroupBy(x => x.IngredientName)
                select new ViewFullRecipe()
                {
                    RecipeName = recipeGroup.Key,
                    ReceiptWeight = ????

如何获得 RecipeWeight 的值?谢谢,

4

1 回答 1

3

LINQ 确实有 sum

from d in dataset
group d by new { d.RecipeName } into g
select new {
     g.Key.RecipeName,
     ReceiptWeight = g.sum(o => o.ReceiptWeight)
}
于 2013-04-24T02:30:32.850 回答