Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, you can do this with LINQ, I'd do this:</p> <pre><code>var dataRows1 = st1.AsEnumerable().Select(r =&gt; new { Id = r["id"], Val1 = r["val1"], Val2 = r["val2"] }); var dataRows2 = st2.AsEnumerable().Select(r =&gt; new { Id = r["id"], Val1 = r["val1"], Val2 = r["val2"] }); </code></pre> <p>This gives you <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" rel="nofollow"><code>IEnumerable&lt;T&gt;</code></a> of anonymous types which can be compared against. Then, you can do this:</p> <pre><code>var dt2NotInDt1 = dataRows2.Where(r2 =&gt; !dataRows1.Any(r1 =&gt; r1.Equals(r2)); </code></pre> <p>Note, that this assumes that your comparison criteria is all the values in the rows (<code>id</code> included).</p> <p>Also note the call to <a href="http://msdn.microsoft.com/en-us/library/system.object.equals.aspx" rel="nofollow"><code>Equals</code></a>. Anonymous types override the <code>Equals</code> method to provide a value comparison across all the properties of the anonymous type. From the <a href="http://msdn.microsoft.com/en-us/library/bb397696.aspx" rel="nofollow">"Anonymous Types" section of the C# Programming Guide</a>:</p> <blockquote> <p>Because the <code>Equals</code> and <a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx" rel="nofollow"><code>GetHashCode</code></a> methods on anonymous types are defined in terms of the <code>Equals</code> and <code>GetHashcode</code> methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal.</p> </blockquote> <p>You can simplify this logic depending on what the constraints are. For example, if there is a primary key (or some sort of unique row identifier) you could place the rows in a dictionary keyed on that value and then perform the lookup based on that.</p> <p>In your case, assuming <code>id</code> is unique, you could do this (after the first two lines at the top):</p> <pre><code>var dataRows1Map = dataRows1.ToDictionary(r =&gt; r.Id); </code></pre> <p>That will give you a map which you can then do a lookup on (I'm switching to query syntax, as I believe it's a little more readable here).</p> <pre><code>var dt2NotInDt1 = for r2 in dataRows2 let r1Exists = dataRows1Map.ContainsKey(r2.Id) let r1 = r1Exists ? dataRows1Map[r2.Id] : null where // Rows that don't have a primary key in the first set. !r1Exists || // Rows that are different. !r1.Equals(r2) select r2; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload