2011-07-01 133 views
7

我在數據表中有三列:string,DateTime和decimal。我想按字符串和小數列進行分組,對於分組的行,我想對小數值進行求和。我知道如何完成總和部分,但是如何將數據表中的兩個不同列分組?c#使用linq按數據表中的多列分組

這是到目前爲止我的代碼不正常工作:

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")) 
       }; 
+1

「編譯器認爲所有的obj ects是相同的,所以每一行都被分組「 - 你的意思是所有的對象都被放到一個大的組中,所以最終只有'newSort'集合中有一個項目? – StriplingWarrior

+0

是的,newSort只包含數據表的最後一個元素 – Sam

+0

該查詢對我來說看起來是正確的。你能檢查並重新檢查你的輸入嗎? – StriplingWarrior

回答

14

我不認爲你給我們完整的故事。除了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 
+0

想知道如何根據兩個其他列的MIN值合併,從上面的每個組中拉出一行? – Si8

+0

@ Si8:'grp.OrderBy(g => g.col1 + g.col2).FirstOrDefault()'? – StriplingWarrior

3

使用此代碼

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])) 
      }; 
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