none
Trying to Delete Values from a DataTable RRS feed

  • Question

  • So I have a DataTable that is a copy of a Database, and I need to delete all Rows in the DataTable unless - if the values in the second column of one row is different than the previous row and the store number is the same, save those two rows and the next consecutive row. Else, if the values don't change delete them.

    I have a method, but I just need verification that it works. Im encountering problems later in my program and I am not 100% sure this code works the way I want, could somebody explain what is happening here? I contributed to this code, but I am fairly new to C# so I am not 100% sure on it 

    public void DeleteFromDataTable(DataTable table)
            {
                var rows = table.Rows.OfType<DataRow>();
                var stores = rows.GroupBy(r => r.Field<string>(0));
    
                foreach (var store in stores)
                {
                    //Enumerate the rows associated with the store
                    DataRow previousRow = null;
                    var autoFreezeRow = false;
    
                    //Track the rows we don't want to remove
                    var frozenRows = new List<DataRow>();
    
                    foreach (var row in store)
                    {
                        //If the current row should be auto frozen then do that now
                        if (autoFreezeRow)
                        {
                            frozenRows.Add(row);
                            autoFreezeRow = false;
                        }
                        else if (previousRow != null)
                        {
                            //If the previous and current rows don't have the same value then we'll freeze the previous, current and next rows                
                            //so they won't get removed later
                            if (previousRow.Field<double>(1) != row.Field<double>(1) && previousRow.Field<string>(0).Equals(row.Field<string>(0)))
                            {
                                frozenRows.Add(previousRow);
                                frozenRows.Add(row);
                                autoFreezeRow = true;
                            };
                        };
    
                        //Next
                        previousRow = row;
                    };
    
                    //Make another pass through and any rows that aren't frozen are marked for deletion
                    var rowsToDelete = store.Except(frozenRows);
                    foreach (var row in rowsToDelete)
                        row.Delete();
                }
            }

    Tuesday, July 31, 2018 2:24 PM

Answers

  • Hi JxkeZ,

    According to your description, I create a simple demo and test it, it seems that the code works as expect. Do you encounter any issues? 

    Best regards,

    Zhanglong


    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.

    Wednesday, August 1, 2018 5:49 AM
    Moderator

All replies

  • Hi JxkeZ,

    According to your description, I create a simple demo and test it, it seems that the code works as expect. Do you encounter any issues? 

    Best regards,

    Zhanglong


    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.

    Wednesday, August 1, 2018 5:49 AM
    Moderator
  • Hi JxkeZ,

    According to your description, I create a simple demo and test it, it seems that the code works as expect. Do you encounter any issues? 

    Best regards,

    Zhanglong


    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.

    I am encountering issues, but if your demo works fine than my issues must be recurring outside of this portion. Thank you though

    Monday, August 6, 2018 1:16 PM