从一个数据表中删除另一个数据表中存在条目的行

发布于 2024-12-13 15:41:54 字数 1010 浏览 2 评论 0原文

抱歉,主题行令人困惑:)

我想用我的 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 技术交流群。

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

发布评论

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

评论(3

梦言归人 2024-12-20 15:41:54

我认为这会做到这一点:

DataTable tblBadValues; // filled however
DataTable tblAllValues; // filled however
tblBadValues.Merge(tblAllValues); // this will add to tblBadValues all records 
                                  // that aren't already in there
DataTable tblResults = tblBadValues.getChanges(); // this will get the records
    // that were just added by the merge, meaning it will return all the records
    // that were originally in tblAllValues that weren't also in tblBadValues
tblBadValues.RejectChanges(); // in case you need to re-use tblBadValues

I think this will do it:

DataTable tblBadValues; // filled however
DataTable tblAllValues; // filled however
tblBadValues.Merge(tblAllValues); // this will add to tblBadValues all records 
                                  // that aren't already in there
DataTable tblResults = tblBadValues.getChanges(); // this will get the records
    // that were just added by the merge, meaning it will return all the records
    // that were originally in tblAllValues that weren't also in tblBadValues
tblBadValues.RejectChanges(); // in case you need to re-use tblBadValues
壹場煙雨 2024-12-20 15:41:54

使用 Linq to dataset

var badValues = new HashSet<Tuple<int, int>>(
                  tableReadFromFile.AsEnumerable().
                                    Select(row => 
                                      new Tuple<int, int>(row.Field<int>("id1"), row.Field<int>("id2"))));

var result = tableReadFromSql.AsEnumerable().
                                    Where(row => !(badValues.Contains(
                                    new Tuple<int, int>(row.Field<int>("id1"), row.Field<int>("id2")))));

第一个语句基本上创建元组的哈希集这代表了坏价值观。

第二个在第二个表中搜索 id 不在哈希集中的行。

Using Linq to dataset:

var badValues = new HashSet<Tuple<int, int>>(
                  tableReadFromFile.AsEnumerable().
                                    Select(row => 
                                      new Tuple<int, int>(row.Field<int>("id1"), row.Field<int>("id2"))));

var result = tableReadFromSql.AsEnumerable().
                                    Where(row => !(badValues.Contains(
                                    new Tuple<int, int>(row.Field<int>("id1"), row.Field<int>("id2")))));

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.

往事风中埋 2024-12-20 15:41:54

我有一个想法,尽管您必须执行 LINQ to SQL。

var query = from data in AllObjects                                    
                    select data;

foreach (DataObject o in BadData)
{
    DataObject temp = o;
    query = query.Where(x => !((x.id1 == temp.id1) && (x.id2 == temp.id2)));
}
//query now contains the expression to get only good rows.

仅当 query 被迭代(或 .ToArray 等)时,它才会执行对数据库服务器的调用。

I have an idea, although you would have to do LINQ to SQL.

var query = from data in AllObjects                                    
                    select data;

foreach (DataObject o in BadData)
{
    DataObject temp = o;
    query = query.Where(x => !((x.id1 == temp.id1) && (x.id2 == temp.id2)));
}
//query now contains the expression to get only good rows.

Only when query gets iterated (or .ToArray etc.) does it execute a call to you database server.

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