LINQ to Datatable Group by并返回所有列

时间:2013-03-16 10:36:52

标签: c# linq

我有一个数据表

ID  Age Last name   First Name  Sex class   Father_Name Mother_Name Marks
1   20  A   B   M   1   A   B   50
1   20  A   B   M   1   A   B   50
1   20  A   B   M   1   A   B   100
2   15  F   G   F   2   H   J   40
2   15  F   G   F   2   H   J   50

&安培;我希望通过

应用Linq Group后得到以下结果
1   20  A   B   M   1   A   B   200
2   15  F   G   F   2   H   J   90

我能够获得总和,但我无法通过

获取LINQ组中的其他列

任何人都可以帮忙吗

var drResults = from r1 in dtResults.AsEnumerable()
                            group r1 by new 
                                {
                                   MarksSum=  r1.Field<int>("Marks"),

                                }
                                into g
                            select new
                            {


                                    Total = g.Sum(r1=>r1.Field<int>("Marks"))                                   
                            };

3 个答案:

答案 0 :(得分:6)

var _result =   from r1 in dtResults.AsEnumerable()
                group r1 by new
                {
                    ID = r1.Field<int>("ID"),
                    Age = r1.Field<int>("Age"),
                    LastName =  r1.Field<int>("LastName"),
                    FirstName = r1.Field<int>("FirstName"),
                    Sex =  r1.Field<int>("Sex"),
                    Class =  r1.Field<int>("class"),
                    Father_Name =  r1.Field<int>("Father_Name"),
                    Mother_Name =  r1.Field<int>("Mother_Name")
                } into g
                select new
                {
                    ID = g.Key.ID,
                    Age = g.Key.Age,
                    LastName =  g.Key.LastName,
                    FirstName = g.Key.FirstName,
                    Sex =  g.Key.Sex,
                    Class =  g.Key.Class,
                    Father_Name =  g.Key.Father_Name,
                    Mother_Name =  g.Key.Mother_Name,
                    TotalMark = g.Sum(x => x.Field<int>("Marks"))
                };

答案 1 :(得分:4)

您希望按Id分组?!然后,您可以在论坛中使用Sum Marks

var result = from r in dtResults.AsEnumerable()
             group r by r.Field<int>("ID") into IdGroup
             let row = IdGroup.First()
             select new
             {
                 ID = IdGroup.Key,
                 Age = row.Field<int>("Age"),
                 LastName = row.Field<string>("Last_Name"),
                 FirstName = row.Field<string>("First_Name"),
                 MotherName = row.Field<string>("Mother_Name"),
                 FatherName = row.Field<string>("Father_Name"),
                 Marks = IdGroup.Sum(r => r.Field<int>("Marks"))
             };

<强>更新

  

嘿蒂姆,如果所有行都有不同的值,那我该怎么办?例如   LastName不同,ID相同。我有一些数据   在这种情况下我要问的是腐败还是可以说是多余的。

显示如何“修复”DataTable这个问题超出了这个问题的范围。但是,如果您想要列出所有行,即使它们具有相同的ID,但是如果每个Id-Group正确总计Mark,则可以使用此查询:

var result = from r in dtResults.AsEnumerable()
             group r by r.Field<int>("ID") into IdGroup
             let IdTotalMarks = IdGroup.Sum(r => r.Field<int>("Marks"))
             from row in IdGroup
             select new
             {
                 ID = IdGroup.Key,
                 Age = row.Field<int>("Age"),
                 LastName = row.Field<string>("Last Name"),
                 FirstName = row.Field<string>("First Name"),
                 MotherName = row.Field<string>("Mother Name"),
                 FatherName = row.Field<string>("Father_Name"),
                 Marks = IdTotalMarks
             };

答案 2 :(得分:1)

JW使用Lambda语法的答案(我还将字段重命名为“class”,因为这不会编译):

       var _result = dtResults
            .AsEnumerable()
            .GroupBy(r1 => new
            {
                ID = r1.Field<int>("ID"),
                Age = r1.Field<int>("Age"),
                LastName =  r1.Field<int>("LastName"),
                FirstName = r1.Field<int>("FirstName"),
                Sex =  r1.Field<int>("Sex"),
                Class =  r1.Field<int>("class"),
                Father_Name =  r1.Field<int>("Father_Name"),
                Mother_Name =  r1.Field<int>("Mother_Name")
            }).Select(g => new
            {
                ID = g.Key.ID,
                Age = g.Key.Age,
                LastName =  g.Key.LastName,
                FirstName = g.Key.FirstName,
                Sex =  g.Key.Sex,
                Class =  g.Key.Class,
                Father_Name =  g.Key.Father_Name,
                Mother_Name =  g.Key.Mother_Name,
                TotalMark = g.Sum(x => x.Field<int>("Marks"))
            });