创建具有不同条目的 DataTable 或 DataView

发布于 2024-09-26 03:22:14 字数 960 浏览 1 评论 0原文

我有以下 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
Admin 8.9

由于 HR 重复了两次,所以我对其进行了平均。

我想在从 XML 文件创建 DataTable 或 DataView 时指定此条件。怎么办?

I have the following XML file

<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>

I am able to read the XML into a DataTable and create a DataView out of it. But I want to do the following:

Wherever Category is repeated (like HR is repeated twice), consider only one record and average the performance points. So the record should be like

HR 7.7
Marketing 8.8
Admin 8.9

Here since HR was repeated twice, I averaged it out.

I want to specify this condition while creating a DataTable or DataView out of the XML file. How to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2024-10-03 03:22:14

您是否尝试过 .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

Did you try .ToTable()?

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

Checkout:

--EDIT--
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()
                 };

Gives:

HR 7.7

Marketing 8.8

Admin 8.9

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文