Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:21:42+00:00 2026-06-12T15:21:42+00:00

I have two tables such as DTTable1 and DTTable2. It has the following records.

  • 0

I have two tables such as DTTable1 and DTTable2. It has the following records.

DTTable1:

    ItemID     Specification    Amount
   ---------  ---------------  ---------
      1             A             10
      1             B             20
      1             C             30

DTTable1:

    ItemID     Specification    Amount
   ---------  ---------------  ---------
      1             A             10
      1             B             20
      1             C             30
      2             A             10
      2             B             20
      3             A             10
      3             B             20

Here I want to compare these two tables. If DTTable1 records present in DTTable2(consider only ItemID) then remove the corresponding rows which has the ItemID same as DTTable1.

I have tried foreach and forloop.
Using ForEach:

   foreach (DataRow DR in DTTable2.Rows)
   {
      if (DR["ItemID"].ToString() == DTTable1.Rows[0]["ItemID"].ToString())
      {
           DTTable2.Rows.Remove(DR);                            
      }
   }
   DTTable2.AcceptChanges();

It showed the error,
“Collection was modified; enumeration operation might not execute”. So I used For Loop, It also not given the desired result.

Using For Loop:

   for (int i = 0; i < DTTable2.Rows.Count; i++)
   {
       if (DTTable2.Rows[i]["ItemID"].ToString() == DTTable1.Rows[0]["ItemID"].ToString())
       {
           DTTable2.Rows.RemoveAt(i);
       }
   }
   DTTable2.AcceptChanges();

But sometimes, the second row doesn’t remove from the table. I get the final DataTable as

    ItemID     Specification    Amount
   ---------  ---------------  ---------
      1             B             20
      2             A             10
      2             B             20
      3             A             10
      3             B             20

How to solve this? What is the simplest method to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:21:44+00:00Added an answer on June 12, 2026 at 3:21 pm

    You can’t modify a collection while you are enumerating it as in your first example. You should instead get a list of the rows to delete and then delete them.

    List<DataRow> rowsToDelete = new List<DataRow>();
    foreach (DataRow DR in DTTable2.Rows)
    {
        if (DR["ItemID"].ToString() == DTTable1.Rows[0]["ItemID"].ToString())
            rowsToDelete.Add(DR);           
    }
    
    foreach (var r in rowsToDelete)
        DTTable2.Rows.Remove(r);
    

    Or, inline using linq:

    DTTable2.Rows
        .Where(DR => DR["ItemID"].ToString() == DTTable1.Rows[0]["ItemID"].ToString())
        .ToList()
        .ForEach(r => DTTable2.Rows.Remove(r));
    

    Your second example fails because once a row is removed, the indexes of subsequent rows change but you continue to increment i, which effectively skips over the row immediately following the deleted row. There are two ways around this:

    for (int i = DTTable2.Rows.Count - 1; i >= 0; i--)
        if (DTTable2.Rows[i]["ItemID"].ToString() == DTTable1.Rows[0]["ItemID"].ToString())
            DTTable2.Rows.RemoveAt(i);
    

    Or

    int i = 0;
    while (i < DTTable2.Rows.Count)
    {
        if (DTTable2.Rows[i]["ItemID"].ToString() == DTTable1.Rows[0]["ItemID"].ToString())
            DTTable2.Rows.RemoveAt(i);
        else
            i++;
    }
    

    Side note: I wonder if you really mean to always compare to row 0’s data, given your description. Perhaps you meant to compare all rows like the following instead (although not optimal at all)?

    if (DTTable1.Any(r => DTTable2.Rows[i]["ItemID"].ToString() == r["ItemID"].ToString()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables such as STR_IndentHeader and STR_IndentDetail. STR_IndentDetail: IndentID ItemID ItemStatusID --------
I have two tables: Departments --< Employees Department table has data such as: id,
I have two tables in my MySQL database (table1 and table 2). I want
I have two tables with a variable amount of columns. (I don't know how
I have two tables: **Product** ID Name SKU **Brand** ID Name Product table has
Lets say I have two tables as such: People Id Name FirstDate LastDate --
I have two tables, one contains a lot of product information such as product_name
I have two tables here both with their respective scaffold generated controllers and models:
I have two tables, one with categories and subcategories. Each category and subcategory has
I have two tables. Widgets, which has information about each widget (Color, size, etc);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.