简体   繁体   English

.NET DataSet查询效率

[英].NET DataSet query efficiency

I've got a xml that I'm parsing and trying to extract some data from. 我有一个要解析的XML,并试图从中提取一些数据。 Let's say the resulting dataset, after parsing an input xml file, has (2) Tables. 假设生成的数据集在解析输入的xml文件后具有(2)个表。

Table #1 contains an IP Address and a primary key. 表#1包含IP地址和主键。 Table #2 contains port numbers and a matching primary key. 表#2包含端口号和匹配的主键。

I want to go through both tables and generate an object that contains an IP Address and matching port. 我想浏览两个表并生成一个包含IP地址和匹配端口的对象。 Basically merging data from two tables that share the same primary key. 基本上合并来自共享相同主键的两个表中的数据。

Right now, I'm using a foreach loop nested within another foreach loop. 现在,我正在使用嵌套在另一个foreach循环中的foreach循环。 The outer one goes through each IP Address and the inner one goes through each port and matches the same primary key. 外部的一个通过每个IP地址,内部的一个通过每个端口并匹配相同的主键。

The result works, but it's O(n^2). 结果有效,但是它是O(n ^ 2)。 Is there a faster way to do this? 有更快的方法吗?

btw, I'm using C# 顺便说一句,我正在使用C#

First, ensure that you set the PrimaryKey property of each DataTable to the appropriate column. 首先,确保将每个DataTablePrimaryKey属性设置为适当的列。 Then, instead of the inner loop, use table.Rows.Find(primaryKeyValue) to pull out the appropriate row from the second DataTable . 然后,使用table.Rows.Find(primaryKeyValue)代替内部循环,从第二个DataTable拉出适当的行。 On both the NT and Compact frameworks, this will create and use a red-black tree index internally, giving you O(n log n) time. 在NT和Compact框架上,这都会在内部创建并使用红黑树索引,从而为您提供O(n log n)时间。

To get to O(n), you need to create a Dictionary (implemented internally as a hash table ) of the rows in the second table and perform your lookup on this. 要获得O(n),您需要为第二个表中的行创建一个Dictionary (在内部实现为哈希表 ),并对此进行查找。 Ensure that you create the Dictionary with sufficient capacity that it will not need to be resized during insertion. 确保您创建的Dictionary具有足够的容量 ,在插入期间无需调整其大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM