简体   繁体   English

在C#中查找具有相同ID的数据行

[英]Find datarows with the same id in C#

It is my first time working with Datarows or tabular data. 这是我第一次使用Datarows或表格数据。 Considering I have a simple table with two columns, one contains the ID and the other contains a name string. 考虑到我有一个包含两列的简单表,一列包含ID,另一列包含名称字符串。 What I need to do is find two Datarows with the same ID, and pass these two rows to another function, lets call it HandleTheDuplicates . 我需要做的是找到两个具有相同ID的数据行,并将这两行传递给另一个函数,让我们将其HandleTheDuplicates

At the moment I am trying store the previous ID in another variable and check if this matches to the current ID and then add it to a list of type DataRow. 目前,我正在尝试将先前的ID存储在另一个变量中,并检查它是否与当前ID匹配,然后将其添加到DataRow类型的列表中。

List<DataRow> RowList = new List<DataRow>();
int previous = 0 ;
foreach (DataRow rowToMatch in importSetOfTables.Tables["MyTable"].Rows)
{
     int rowID = Convert.ToInt32(rowToMatch["ID"]);                               
     if (previous == rowID)
     {
          RowList.Add(rowToMatch);
     }
     else
     {
          RowList.Clear();
     }
     previous = rowID;

     if(RowList.Count > 1) //in case of a match
     {
          HandleTheDuplicates(RowList);
     }
}

I don't know if this is the right way of doing something like this or if there could be something more simpler. 我不知道这是做这样的事情的正确方法,还是可能有更简单的方法。

LINQ is a good option here: LINQ是一个不错的选择:

List<DataRow> RowList = new List<DataRow>();

foreach (DataRow row in importSetOfTables.Tables["MyTable"].Rows)
{
    // Use LINQ to get a list of rows matching on ID
    List<DataRow> matches = (from t in importSetOfTables.Tables["MyTable"].Rows
                             where row.ID == t.ID
                             select a).ToList();
    // Insert matching rows into your collection
    if (matches.Count() > 0 )
         if (!(RowList.Contains(matches[0]))
             RowList.AddRange(matches);
}

If you want to remove them from the original collection, another foreach loop might work: (Generally don't want to remove items from a collection you are iterating over) 如果要从原始集合中删除它们,则可能会出现另一个foreach循环:(通常不希望从要迭代的集合中删除项目)

foreach(DataRow row in RowList)
{
    if (importSetOfTables.Tables["MyTable"].Rows.Contains(row))
        importSetOfTables.Tables["MyTable"].Rows.Remove(row);
}

Your code could work if you have the DataTable sorted by ID. 如果您的数据表按ID排序,则您的代码可以工作。 To be sure then set the DataTable.DefaultView.Sort property on the ID field and then create your loop using the DataTable.DefaultView 确保可以在ID字段上设置DataTable.DefaultView.Sort属性,然后使用DataTable.DefaultView创建循环。

List<DataRow> RowList = new List<DataRow>();
int previous = 0 ;
importSetOfTables.Tables["MyTable"].DefaultView.Sort = "ID";
foreach (DataRowView rw in importSetOfTables.Tables["MyTable"].DefaultView)
{
     int rowID = Convert.ToInt32(rw["ID"]);
     if (previous == rowID)
          RowList.Add(rw.Row);
     else
     {
          // Call the function that handles the duplicates
          if(RowList.Count > 1) 
             HandleTheDuplicates(RowList);
          RowList.Clear();
     }
     previous = rowID;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM