2013-04-24 79 views
2

我試圖找到一個遠來概括所有的數量爲特定方法(所有成分)爲一個值來獲得總量如何在LINQ中對另一個字段進行分組?

假設我有以下數據集:

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的值? 謝謝,

回答

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) 
} 
相關問題