Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>var requirement1 = list1.Intersect(list2); var requirement2 = list1.Except(list2); var requirement3 = list2.Except(list1); </code></pre> <p>For your <code>List&lt;string&gt;</code>, this will be all you need. If you were doing this for a custom class and you were looking for something other than reference comparisons, you'd want to ensure that the class properly overrided <code>Equals</code> and <code>GetHashCode</code>. Alternatively, you could provide an <code>IEqualityComparer&lt;YourType&gt;</code> to overloads of the above methods. </p> <p><strong>Edit:</strong> </p> <p>OK, now you've indicated in the comments that it isn't a list of string, it's a <code>List&lt;MyObject&gt;</code>. In which case, override Equals/GetHashCode (if your key should uniquely identify these classes all the time and you have access to the source code) or provide an IEqualityComparer implementation (still involves Equals/GetHashCode, use this if the comparison is unique to these requires or if you do not have access to MyObject source). </p> <p>For example: </p> <pre><code>class MyObjectComparer : IEqualityComparer&lt;MyObject&gt; { public bool Equals(MyObject x, MyObject y) { // implement appropriate comparison of x and y, check for nulls, etc } public int GetHashCode(MyObject obj) { // validate if necessary return obj.KeyProperty.GetHashCode(); } } </code></pre> <p>If you used a custom equality comparer such as this, the call to the above methods would be </p> <pre><code>list1.Intersect(list2, customComparerInstance); </code></pre> <p><strong>Edit:</strong> And now you've moved the bar yet again, this time the problem deals with two distinct classes. For this, you would consider utilizing join operations, one being an inner, the other being an outer. </p> <p>In the case of </p> <pre><code>class Class1 { public string Foo { get; set; } } class Class2 { public string Bar { get; set; } } </code></pre> <p>You could write </p> <pre><code>var intersect = from item1 in list1 join item2 in list2 on item1.Foo equals item2.Bar select item1; var except1 = from item1 in list1 join item2 in list2 on item1.Foo equals item2.Bar into gj from item2 in gj.DefaultIfEmpty() where item2 == null select item1; </code></pre> <p>To get the items in list2 without matching* objects in list1, simply reverse the order of the lists/items in the <code>except1</code> query.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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