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には合計があります

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 に答える