2010-10-06 61 views
0

我有下面的XML文件創建一個DataTable或數據視圖與鮮明的條目

<Department> 
    <dept> 
     <category>HR</category> 
     <performance>8.2</performance> 
     <month>3</month> 
    </dept> 
    <dept> 
     <category>Marketing</category> 
     <performance>8.8</performance> 
     <month>3</month> 
    </dept> 
    <dept> 
     <category>HR</category> 
     <performance>7.2</performance> 
     <month>4</month> 
    </dept> 
    <dept> 
     <category>Admin</category> 
     <performance>8.9</performance> 
     <month>4</month> 
    </dept> 
</Department> 

我能夠讀取XML到一個DataTable,並創建一個DataView出來。但我想要做到以下幾點:

無論何處重複類別(如HR重複兩次),只考慮一條記錄並平均性能點。因此,記錄應該像

HR 7.7
營銷8.8
管理員8.9

在這裏,因爲HR重複兩次,我平均出來。

我想在從XML文件中創建DataTable或DataView時指定此條件。怎麼做?

+0

結帳編輯。 – 2010-10-06 10:33:59

回答

0

你試過.ToTable()

DataTable distinctTable = originalTable.DefaultView.ToTable(/*distinct*/ true); 

結帳:

- 編輯 - http://izlooite.blogspot.com/2010/10/how-to-distinct-sum-count-datatable-for.html

var result = from theRow in dtTable.DataSet.Tables[0].AsEnumerable() 
       group theRow by new 
       { 
        theRow = theRow.Field<string>("category")//Group by category. 
       } into Group 
       select new 
       { 
        Row = Group.Key.theRow, 
        Count = Group.Count(), 
        Average = Group.Average(row => Decimal.Parse(row.Field<string>("performance"))), 
        UniqueRows = (from p in Group 
            select p.Field<string>("performance")).Distinct().Count() 
       }; 

給出:

HR 7.7

營銷8.8

管理員8.9

相關問題