2013-04-24 77 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) 
} 
相关问题