比较数据集或更好的想法

发布于 2024-07-25 03:24:32 字数 486 浏览 3 评论 0原文

如何比较一个数据集与另一个数据集的值。

第一个数据集 [“正确记录”] 来自 SQL Server,列名称

 [id], [subsNumber]

第二个数据集 [“正确和不正确记录”] 来自进度数据库,具有不同的列,除了1 是 subsNumber

我如何去创建另一个数据集,其中包含 ["properrecords"] 中的所有 [subsNumber] 以及第二个数据集 ["proper inproper] 中的匹配记录记录”] ?

删除第二个数据集中的所有记录[“正确和不正确的记录”],这些记录与第一个数据集中的“subsNumber”列

或任何其他想法

基本上不匹配如何从具有相同“subsNumber”的第二个数据集中获取所有记录作为第一个数据集

How do I compare values of one data set from another.

1st dataset ["proper records"] is coming from SQL Server with column names

 [id], [subsNumber]

2nd dataset ["proper and inproper records"] is coming from progress database, with different columns except 1 which is subsNumber

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"] ?

or

delete all the records in 2nd dataset["proper and inproper records"] which don't match the "subsNumber" column in the 1st dataset

or any other idea

basically How do I get all records from 2nd dataset which has same "subsNumber" as the 1st dataset

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

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

发布评论

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

评论(3

羞稚 2024-08-01 03:24:32

关键是使用 System.Data.DataRelation 将 2 个数据表连接到一个公共列(或多个列)上。

以下是源自 KC's See 的帖子的一些代码夏普博客

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

The key is using System.Data.DataRelation to join your 2 datatables on a common column (or columns).

Here's some code derived from a post at KC's See Sharp Blog

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;
}
不甘平庸 2024-08-01 03:24:32

我解决了问题:

第一个数据集--> 循环遍历并获取 subsNumber

调用函数并传递 subsNumber 和第二个数据集 --> 到它
然后为新数据集启动另一个循环

如果子编号不匹配则继续
如果 subsNumber 匹配该数据,例如将列添加到 sqlserver 表等

代码:

 foreach (DataRow row in ecommDS.Tables["EcommData"].Rows)
 {           
     //string statCode = ""
     string prdCode = ""; //declaring var for getting string format from ecomm
     string checking = "";
     prdCode = row["PRD-CDE"].ToString();
     checking = row["SUBS-NUM"].ToString();

     if(checking != subsNum)
     {
         continue;
     }

I solved the problem:

1st dataset--> loop throuhg and get the subsNumber

Call function and pass subsNumber and 2nd dataset--> to it
Then start another loop for new dataset

Continue if subsnumber don't match
If subsNumber match work on that data like add columns to sqlserver table etc.

code:

 foreach (DataRow row in ecommDS.Tables["EcommData"].Rows)
 {           
     //string statCode = ""
     string prdCode = ""; //declaring var for getting string format from ecomm
     string checking = "";
     prdCode = row["PRD-CDE"].ToString();
     checking = row["SUBS-NUM"].ToString();

     if(checking != subsNum)
     {
         continue;
     }
过去的过去 2024-08-01 03:24:32

要从第二个数据集中获取与第一个数据集中的记录匹配的所有记录,如下所示:

IEnumerable list3 = list2.Where(l2=>list1.Contains(l1=>l1.subsNumber == l2.subsNumber) );

类似的东西!

To get all the records from 2nd dataset that match the records from the 1st dataset would be something like this:

IEnumerable list3 = list2.Where(l2=>list1.Contains(l1=>l1.subsNumber == l2.subsNumber));

Something along those lines!

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