我有以下XML文件
<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
由于人力资源重复了两次,我将其平均化了。
我想在从XML文件创建DataTable或DataView时指定此条件。怎么办?
答案 0 :(得分: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