从一个数据表中删除另一个数据表中存在条目的行
抱歉,主题行令人困惑:)
我想用我的 DataTable:s 进行类似 SQL 的查询:我想做这样的事情
// Is named "BadValues" Rows contain: id1, id2
DataTable tableReadFromFile = readFromFile();
// Is named "AllValues" Rows contain id1, id2
DataTable tableReadFromSql = readFromSql
DataTable resultTable =
tableReadFromFile.select("where AllValues.id1 not in (select id1 from BadValues) and AllValues.id2 not in (select id2 from BadValues)");
所以如果我的“BadValues”表看起来像这样:
id1 id2
0 1
10 11
20 21
而我的“AllValues”表看起来像这样:
id1 id2
0 1
0 2
1 1
10 11
10 12
12 11
20 21
20 22
22 21
我希望 resultTable 看起来像这样:
id1 id2
0 2
1 1
10 12
12 11
20 22
22 21
换句话说:如果 id1,id2 对存在于表“BadValues”和“AllValues”中,我想删除它们,以便它们不存在于结果表中。
如果 SQL 数据库中存在表“BadValues”,那么在 SQL 中执行此操作会相当简单,但由于它是从文件加载的,所以这是不可能的。
现在,我循环遍历“BadValues”中的所有行,并使用 id1 和 id2 值集构造单独的 SQL 查询。由于我有很多数据,所以非常耗时。
任何提示表示赞赏!
Sorry about the confusing subject line :)
I want to make a SQLlike query with my DataTable:s: I want to do something like this
// Is named "BadValues" Rows contain: id1, id2
DataTable tableReadFromFile = readFromFile();
// Is named "AllValues" Rows contain id1, id2
DataTable tableReadFromSql = readFromSql
DataTable resultTable =
tableReadFromFile.select("where AllValues.id1 not in (select id1 from BadValues) and AllValues.id2 not in (select id2 from BadValues)");
So if my "BadValues" table would look like this:
id1 id2
0 1
10 11
20 21
and my "AllValues" table would look like this:
id1 id2
0 1
0 2
1 1
10 11
10 12
12 11
20 21
20 22
22 21
I would like the resultTable to look like this:
id1 id2
0 2
1 1
10 12
12 11
20 22
22 21
In other words: if the pair id1,id2 exists in the table "BadValues" and in "AllValues" I want to remove them so that they don't exist in the result table.
This would have been rather simple to do in SQL if the table "BadValues" would exist in the SQL database, but since it is loaded from file that is not possible.
As it is now, I loop through all rows in the "BadValues" and construct individual SQL queries with the id1 and id2 values set. Since I have quite a lot of data, that is very time consuming.
Any tip is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这会做到这一点:
I think this will do it:
使用 Linq to dataset:
第一个语句基本上创建元组的哈希集这代表了坏价值观。
第二个在第二个表中搜索 id 不在哈希集中的行。
Using Linq to dataset:
The first statement basically creates a hashset of the tuples which represent the bad values.
The second searches in the second table the rows which ids are not in the hashset.
我有一个想法,尽管您必须执行 LINQ to SQL。
仅当
query
被迭代(或.ToArray
等)时,它才会执行对数据库服务器的调用。I have an idea, although you would have to do LINQ to SQL.
Only when
query
gets iterated (or.ToArray
etc.) does it execute a call to you database server.