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;
}
这篇关于比较数据集或更好的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较数据集或更好的想法
基础教程推荐
- 将数据集转换为列表 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
