格式化列表数据合并列

时间:2014-07-16 09:31:07

标签: c# linq

例如,如何使用LINQ将GroupId对以下记录进行分组,并(从而将每个组中的所有行合并为一个)

Id| Name    | FatherName|Nmber      |Clone   |Demand | Supp  |Survival 
1 | Farmer1 | FFather1  |9876543210 |G-48    |200    |  200  |150
1 | Farmer1 | FFather1  |9876543210 |L-48/49 |150    |  150  |150
1 | Farmer1 | FFather1  |9876543210 |Uday    |400    |  400  |300
3 | Farmer23| FFather3  |9876543210 |G-48    |1212   |1212   |1210          
3 | Farmer23| FFather3  |9876543210 |L-48/49 |2121   |2121   |2000

请让我知道如何使用分隔的逗号添加这些不添加这些值预期的结果是:     需要OutPut:

Id|Name    | Father  | Number    |G-48=/de/sup/sur|L-48/49=de/sup/sur|Uday=de/sup/sur
1 |Farmer1 | FFather1| 9876543210|200,200,150     |150,150,150       |400,400,300
3 |Farmer23| FFather3| 9876543210|1212/1212/1210  |2121/2121/2000    |null

请帮助告诉我如何合并此预期结果

我试过但没有准确

var data = result.GroupBy(i => i.id).Select(g => new
                {
                    Id= g.Key,
                    FarmerName = g.Select(i => i.FarmerName ).FirstOrDefault(),
                    FatherName = g.Select(i => i.FatherName ).FirstOrDefault(),
                    Number= g.Select(i => i.Number).FirstOrDefault(),
                    Demand = String.Join(",", g.Select(i => i.Demand)),
                    Supply= String.Join(",", g.Select(i => i.Supply)),
                    Survival= String.Join(",", g.Select(i => i.NoOfPlantSurviving)),
                }).ToList();

0 个答案:

没有答案