Removing duplicate entries from a DataTable that exist in another DataTable?
<p>You'd think that it would be simple, isn't there something simple to do this? All I want to do is to remove some duplicate rows. I can obviously save off the PK into a separate list and iterate through that to delete the rows, and may be what I end up doing. It just seems like I'm missing some obvious solution here.</p>

<pre><code>        foreach (var v in FinalData.Visitors)
        {
            bool isDup = (AlreadyCommittedData.Visitors.Any(vdup =&gt; vdup.vid == v.vid));
            if (isDup)
            {
                v.Delete();
            }
        }
</code></pre>

<h2>Fails with 'foreach collection was modified enumeration operation may not execute'</h2>

<p>Two minutes later after googling... My second try was something like...</p>

<pre><code>        var vDup = new LMDB.VisitorsDataTable();

        foreach (var v in FinalData.Visitors)
        {
            bool isDup = (AlreadyCommittedData.Visitors.Any(vdup =&gt; vdup.vid == v.vid));
            if (isDup)
            {
                vDup.AddVisitorRow(v);
            }
        }
        foreach (var v in vDup)
        {
            FinalData.Visitors.Remove(v);
        }
</code></pre>

<h2>Gave me an error something like...Row exists in another table</h2>

<p>My third try is simple brute force ;) Since the duplicates are all at the front, it actually may perform pretty well.</p>

<p>//Still being tested, there may be a typo, if so, please ignore it.
            bool allDone;</p>

<pre><code>        do
        {
            allDone = true;
            foreach (var v in FinalData.Visitors)
            {
                bool isDup = (AlreadyCommittedData.Visitors.Any(vdup =&gt; vdup.vid == v.vid));
                if (isDup)
                {
                    v.Delete();
                    allDone = false;
                    break;
                }
            }

        }
        while (!allDone);
</code></pre>
