C# 创建具有不同条目的DataTable或DataView
我有以下XML文件C# 创建具有不同条目的DataTable或DataView,c#,ado.net,C#,Ado.net,我有以下XML文件 <Department> <dept> <category>HR</category> <performance>8.2</performance> <month>3</month> </dept> <dept> <category>Marketing</category&g
<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>
人力资源
8.2
3.
营销
8.8
3.
人力资源
7.2
4.
管理
8.9
4.
我能够将XML读入DataTable并从中创建DataView。但我想做以下几点:
<>无论重复什么类别(像HR重复两次),只考虑一个记录并平均性能点。所以记录应该是这样的
HR 7.7市场营销8.8
管理8.9 在这里,因为HR重复了两次,所以我求出了平均值 我想在用XML文件创建DataTable或DataView时指定此条件。怎么办?你试过了吗 结帐:
- 或者如果您正在使用Linq,请参阅
var result=来自dtTable.DataSet.Tables[0]中的row
按新分组
{
theRow=theRow.Field(“category”)//按类别分组。
}分组
选择新的
{
行=Group.Key.theRow,
Count=组。Count(),
Average=Group.Average(row=>Decimal.Parse(row.Field(“performance”)),
UniqueRows=(来自组中的p
选择p.Field(“性能”).Distinct().Count()
};
给出:
人力资源7.7
市场营销8.8
管理8.9
DataTable distinctTable = originalTable.DefaultView.ToTable( /*distinct*/ true);
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()
};