none
Keeping Values Stored in List<DataRow> - Comparing the list to DataTable RRS feed

  • Question

  • I currently have a List<DataRow> that stores DataRows I need to keep.

    List<DataRow> rowsToKeep = new List<DataRow>();
    foreach (DataRow r in newTable.Rows)
    {
        foreach (DataRow r2 in table.Rows)
        {
            if(r == r2){
                rowsToKeep.Add(r);
            }
        }
    }

    But, because of the way the rest of this program is set up, I need to delete the values in rowsToKeep out of the newTable DataTable. What is the most efficient way of doing this? Thanks

    Wednesday, August 22, 2018 1:58 PM

All replies

  • Use LINQ's Except to get the list of rows to remove. Then call foreach to Delete each one.

    var itemsToKeep = …;
    
    var originalItems = …;
    
    //Note that this assumes equality comparison is being used
    var itemsToRemove = originalItems.Except(itemsToKeep);
    foreach (var item in itemsToRemove)
       item.Delete();


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 22, 2018 3:32 PM
    Moderator
  • I've never used LINQ before, can I just copy and paste that into my code and it'll work?
    Wednesday, August 22, 2018 3:57 PM
  • Probably not since I'm using different variable names then you but the method call itself should be about the same. The only question is whether it returns the correct list back as I don't know how your app considers 2 rows to be "the same". If they are based upon some arbitrary column value (like Id) then the existing code won't work, you'll need an equality comparer. However if the rows you're comparing are from the same DataTable then it should work.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 22, 2018 5:12 PM
    Moderator
  • Hi JxkeZ,

    >>But, because of the way the rest of this program is set up, I need to delete the values in rowsToKeep out of the newTable DataTable.

    For your question, I have provided a Demo, which does not use Linq.

    Here is the complete code:

            static void Main(string[] args)
            {
                DataTable newTable = new DataTable();
                DataTable table = new DataTable();
                DataColumn dataColumn = new DataColumn("ID", Type.GetType("System.Int32"));
                DataColumn dataColumn1 = new DataColumn("ID", Type.GetType("System.Int32"));
                newTable.Columns.Add(dataColumn);
                table.Columns.Add(dataColumn1);
                Console.Write("before delete newTable: ");
                for (int i = 0; i < 10; i++)
                {
                    DataRow dataRow = newTable.NewRow();
                    dataRow[0] = i;
                    newTable.Rows.Add(dataRow);
                    Console.Write(dataRow[0] + " " );
                }
                Console.WriteLine();
                Console.Write("table: ");
                for (int i = 0; i < 5; i++)
                {
                    DataRow dataRow = table.NewRow();
                    dataRow[0] = i;
                    table.Rows.Add(dataRow);
                    Console.Write(dataRow[0] + " ");
                }
                Console.WriteLine();
                List<DataRow> rowsToKeep = new List<DataRow>();
                for (int i = 0; i < newTable.Rows.Count; i++)
                {
                    bool flag = true;
                    for (int j = 0; j < table.Rows.Count; j++)
                    {
                        if (newTable.Rows[i][0].Equals(table.Rows[j][0]))
                        {
                            flag = false;
                            rowsToKeep.Add(table.Rows[j]);
                            break;
                        }
                    }
                    if (flag)
                    {
                         newTable.Rows.RemoveAt(i);
                         i--;
                    }
                }
                Console.Write("after delete newTable: ");
                for (int i = 0; i < newTable.Rows.Count; i++)
                {
                    Console.Write(newTable.Rows[i][0] + " ");
                }
                Console.WriteLine();
                Console.Write("rowsToKeep: ");
                foreach (DataRow dr in rowsToKeep)
                {
                    Console.Write(dr[0] + " ");
                }
                Console.WriteLine();
            }
    

    Regards,

    JianGuo


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 23, 2018 5:39 AM