Compare dataset or a better idea(比较数据集或更好的想法)
问题描述
如何比较一个数据集与另一个数据集的值.
How do I compare values of one data set from another.
第一个数据集 ["proper records"] 来自 SQL Server 的列名
1st dataset ["proper records"] is coming from SQL Server with column names
 [id], [subsNumber]
2nd dataset ["proper and inproper records"] 来自进度数据库,除了 1 之外的其他列是 subsNumber
2nd dataset ["proper and inproper records"] is coming from progress database, with different columns except 1 which is subsNumber
如何制作另一个数据集,其中包含来自 ["proper records"] 的所有 [subsNumber] 与来自 2nd datset ["proper inproper records"] 的匹配记录?
How do I go and make another dataset which has all the [subsNumber] from ["proper records"] with matching records from 2nd datset ["proper inproper records"] ?
或
删除第二个数据集中的所有记录[正确和不正确的记录"]与第一个数据集中的subsNumber"列不匹配
delete all the records in 2nd dataset["proper and inproper records"] which don't match the "subsNumber" column in the 1st dataset
或任何其他想法
基本上我如何从与第一个数据集具有相同subsNumber"的第二个数据集中获取所有记录
basically How do I get all records from 2nd dataset which has same "subsNumber" as the 1st dataset
推荐答案
关键是使用 System.Data.DataRelation 将您的 2 个数据表连接到一个(或多个)公共列上.
The key is using System.Data.DataRelation to join your 2 datatables on a common column (or columns).
这里有一些代码来自 KC 的查看 Sharp 博客
public DataTable GetImproperRecords(DataTable ProperRecords, DataTable ImproperRecords) {
  DataTable relatedTable = new DataTable("Difference");
  try {
     using (DataSet dataSet = new DataSet()) {
        dataSet.Tables.AddRange(new DataTable[] { ProperRecords.Copy(), ImproperRecords.Copy() });
        DataColumn properColumn = new DataColumn();
        properColumn = dataSet.Tables[0].Columns[1]; // Assuming subsNumber is at index 1
        DataColumn improperColumn = new DataColumn();
        improperColumn = dataSet.Tables[1].Columns[0]; // Assuming subsNumber is at index 0
        //Create DataRelation
        DataRelation relation = new DataRelation(string.Empty, properColumn, improperColumn, false);
        dataSet.Relations.Add(relation);
        //Create columns for return relatedTable
        for (int i = 0; i < ImproperRecords.Columns.Count; i++) {
           relatedTable.Columns.Add(ImproperRecords.Columns[i].ColumnName, ImproperRecords.Columns[i].DataType);
        }
        relatedTable.BeginLoadData();
        foreach (DataRow parentrow in dataSet.Tables[1].Rows) {
           DataRow[] childrows = parentrow.GetChildRows(relation);
           if (childrows != null && childrows.Length > 0)
              relatedTable.LoadDataRow(parentrow.ItemArray, true);
        }
        relatedTable.EndLoadData();
     }
  }
  catch (Exception ex) {
     Console.WriteLine(ex.Message);
  }
  return relatedTable;
}
                        这篇关于比较数据集或更好的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较数据集或更好的想法
				
        
 
            
        - 如何用自己压缩一个 IEnumerable 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - C# 中多线程网络服务器的模式 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 
						
						
						
						
						
				
				
				
				