比较两个数据集并将值放入新数据集中

发布于 2024-10-10 06:45:52 字数 77 浏览 0 评论 0原文

我正在使用 C# 开发 ASP.NET, 我需要将两个数据集中的数据与两个数据集中的“ID”进行比较,然后将所有匹配的行添加到新数据集中。

I m working on ASP.NET using C#,
i need to compare the data from two DataSets with a "ID" which is in both the DataSets and then add all the matching rows to a New dataset.

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

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

发布评论

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

评论(2

巾帼英雄 2024-10-17 06:45:52

在每个数据集中的表之间设置数据关系,然后使用 GetChildRows 方法查找匹配项,然后将其添加到新的数据集或任何其他数据结构中。有关一些信息,请参阅DataRelation 对象简介例子。

Setup a DataRelation between the the tables in each DataSet, then use the GetChildRows method to find the matches which you could then add to your new DataSet or any other data structure. See Introduction to DataRelation Objects for some examples.

許願樹丅啲祈禱 2024-10-17 06:45:52

这可以通过两个数据表的交集来完成

using System.Data;

public static class DataTableExtensions
{
    public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other)
    {
        return table.AsEnumerable().Intersect(other.AsEnumerable());
    }

    public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other, IEqualityComparer<DataRow> comparer)
    {
        return table.AsEnumerable().Intersect(other.AsEnumerable(), comparer);
    }
}

这是一个 优雅解决方案的无耻移植:)

假设您有两个数据集 set1 和 set2 想要比较。 请发布更多详细信息。

var newtable = set1.Tables[0].Intersect(set2.Tables[0]).CopyToDataTable();

如果这不是您想要的,

This can be done by intersection of two datatables

using System.Data;

public static class DataTableExtensions
{
    public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other)
    {
        return table.AsEnumerable().Intersect(other.AsEnumerable());
    }

    public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other, IEqualityComparer<DataRow> comparer)
    {
        return table.AsEnumerable().Intersect(other.AsEnumerable(), comparer);
    }
}

This is a shameless port of an elegant solution :)

Suppose you have two datasets set1 and set2 which you wanna compare. Do this

var newtable = set1.Tables[0].Intersect(set2.Tables[0]).CopyToDataTable();

Post more details, if this is not what you want.

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