c#使用linq按数据表中的多个列进行分组

时间:2011-07-01 17:48:35

标签: c# linq datatable group-by multiple-columns

我在数据表中有三列:string,DateTime和decimal。我想按字符串和十进制列进行分组,对于分组的行,我想对小数值求和。我知道如何进行求和部分,但是如何在数据表中对两个不同的列进行分组?

到目前为止,这是我的代码无法正常运行:

var newSort = from row in objectTable.AsEnumerable()
              group row by new {ID = row.Field<string>("resource_name"), time1 = row.Field<DateTime>("day_date")} into grp
              orderby grp.Key
              select new
              {
                 resource_name1 = grp.Key.ID,
                 day_date1 = grp.Key.time1,
                 Sum = grp.Sum(r => r.Field<Decimal>("actual_hrs"))
              };

3 个答案:

答案 0 :(得分:17)

我认为你没有给我们完整的故事。除了orderby不使用匿名类型(您提供的代码不会编译)之外,您的查询应该按照您想要的方式工作。我把它放在LINQPad中:

var objectTable = new DataTable();
objectTable.Columns.Add("resource_name",typeof(string));
objectTable.Columns.Add("day_date",typeof(DateTime));
objectTable.Columns.Add("actual_hrs",typeof(decimal));
objectTable.Rows.Add(1, DateTime.Today, 1);
objectTable.Rows.Add(2, DateTime.Today, 2);

var newSort = from row in objectTable.AsEnumerable()
            group row by new {ID = row.Field<string>("resource_name"), time1 = row.Field<DateTime>("day_date")} into grp
            select new
                {
                    resource_name1 = grp.Key.ID,
                    day_date1 = grp.Key.time1,
                    Sum = grp.Sum(r => r.Field<Decimal>("actual_hrs"))
                };
newSort.Dump();

......我得到了这些结果:

resource_name1 | day_date1            | Sum
1              | 7/1/2011 12:00:00 AM | 1 
2              | 7/1/2011 12:00:00 AM | 2 

答案 1 :(得分:4)

使用此代码

var newSort = from row in objectTable.AsEnumerable()
          group row by new {ID = row.Field<string>("resource_name"), time1 =    row.Field<DateTime>("day_date")} into grp
          orderby grp.Key
          select new
          {
             resource_name1 = grp.Key.ID,
             day_date1 = grp.Key.time1,
             Sum = grp.Sum(r => Convert.ToDecimal(r.ItemArray[2]))
          };

答案 2 :(得分:0)

对于那些想要在Vb.net中使用解决方案的人来说,这是一个例子:

Dim workTable As DataTable = New DataTable("Customers")

Dim workCol As DataColumn = workTable.Columns.Add("ID", Type.GetType("System.Int32"))
workTable.Columns.Add("Total", Type.GetType("System.Decimal"))
workTable.Columns.Add("Compra", Type.GetType("System.Decimal"))

Dim row As DataRow = workTable.NewRow()
row("id") = 2
row("total") = 1.5
row("compra") = 3
workTable.Rows.Add(row)
row = workTable.NewRow()

row("id") = 1
row("total") = 1.5
row("compra") = 3.3999999999999999
workTable.Rows.Add(row)
row = workTable.NewRow()
row("id") = 1
row("total") = 1.5
row("compra") = 5
workTable.Rows.Add(row)


Dim detalles As IEnumerable(Of DataRow) = workTable.AsEnumerable()

Dim query = From detalle In detalles.AsEnumerable() _
            Group detalle By grupoClave = New With _
                                        { _
                                            Key .C2 = detalle("id"), _
                                            Key .C4 = detalle("total")} Into g = Group _
                                    Select New With _
                                    { _
                                        .Col2 = g(0).Field(Of Integer)("id"), _
                                        .Col3 = g(0).Field(Of Decimal)("total"), _
                                        .Col4 = g.Sum(Function(fact) fact.Field(Of Decimal)("compra")) _
                                    }

For Each p In query
    Console.WriteLine((p.Col2 & p.Col3 & p.Col4))
Next