Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Sometimes LINQ is not the answer.</strong> This is the kind of problem where I would consider using a <code>HashSet&lt;T&gt;</code> with a custom comparer to reduce the work of performing set operations. HashSets are much more efficient at performing set operations than lists - and (depending on the data) can reduce the work considerably:</p> <pre><code>// create a wrapper class that can accomodate either an Alpha or a Bravo class ABItem { public Object Instance { get; private set; } public int Id { get; private set; } public ABItem( Alpha a ) { Instance = a; Id = a.Id; } public ABItem( Bravo b ) { Instance = b; Id = b.Id; } } // comparer that compares Alphas and Bravos by id class ABItemComparer : IComparer { public int Compare( object a, object b ) { return GetId(a).Compare(GetId(b)); } private int GetId( object x ) { if( x is Alpha ) return ((Alpha)x).Id; if( x is Bravo ) return ((Bravo)x).Id; throw new InvalidArgumentException(); } } // create a comparer based on comparing the ID's of ABItems var comparer = new ABComparer(); var hashAlphas = new HashSet&lt;ABItem&gt;(myAlphaItems.Select(x =&gt; new ABItem(x)),comparer); var hashBravos = new HashSet&lt;ABItem&gt;(myBravoItems.Select(x =&gt; new ABItem(x)),comparer); // items with common IDs in Alpha and Bravo sets: var hashCommon = new HashSet&lt;Alpha&gt;(hashAlphas).IntersectWith( hashSetBravo ); hashSetAlpha.ExceptWith( hashSetCommon ); // items only in Alpha hashSetBravo.ExceptWith( hashSetCommon ); // items only in Bravo </code></pre>
 

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