【问题标题】:compare two datasets and place values in a new dataset比较两个数据集并将值放入新数据集中
【发布时间】:2011-01-06 05:19:05
【问题描述】:

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

【问题讨论】:

  • 您尝试自己解决什么问题?你有什么困难?还是您希望我们为您提供完整的解决方案,而无需进行任何研究?

标签: c# asp.net ado.net


【解决方案1】:

在每个 DataSet 中的表之间设置 DataRelation,然后使用 GetChildRows 方法查找匹配项,然后将其添加到新 DataSet 或任何其他数据结构中。有关示例,请参阅Introduction to DataRelation Objects

【讨论】:

    【解决方案2】:

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

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

    这是shameless port of an elegant solution :)

    假设您有两个要比较的数据集 set1 和 set2。这样做

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

    如果这不是您想要的,请发布更多详细信息。

    【讨论】:

    • 这有什么帮助? intersect 如何定义行关系?
    • " 我需要将来自两个 DataSet 的数据与两个 DataSet 中的“ID”进行比较,如果架构相同,那就是交集。
    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 2015-07-10
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多