Note that there are some explanatory texts on larger screens.

plurals
  1. POCombine two lists of entities with a condition
    primarykey
    data
    text
    <p>Say I have a class defined as </p> <pre><code>class Object { public int ID { get;set; } public string Property { get; set; } public override bool Equals(object obj) { Object Item = obj as Object; return Item.ID == this.ID; } public override int GetHashCode() { int hash = 13; hash = (hash * 7) + ID.GetHashCode(); return hash; } } </code></pre> <p>And two lists, defined like so:</p> <pre><code>List&lt;Object&gt; List1; List&lt;Object&gt; List2; </code></pre> <p>These two lists contain objects where <code>ID</code> fields could be the same, but <code>Property</code> fields may or may not. I want to have a result of all objects contained in <code>List1</code> together with all objects contained in <code>List2, with the condition that</code>Property<code>field must be set to</code>"1"<code>if it is set to</code>"1"` in any of those lists. The result must contain distinct values (distinct IDs).</p> <p>For example, if we have 2 lists like this:</p> <pre><code>List1 ----- ID = 0, Property = "1" ID = 1, Property = "" ID = 2, Property = "1" ID = 3, Property = "" List2 ----- ID = 1, Property = "1" ID = 2, Property = "" ID = 3, Property = "" </code></pre> <p>I need a result to look like this:</p> <pre><code>Result ------- ID = 0, Property = "1" ID = 1, Property = "1" ID = 2, Property = "1" ID = 3, Property = "" </code></pre> <p>Currently it works like this:</p> <pre><code>var Result = List1.Except(List2).Concat(List2.Except(List1)); var Intersection = List1.Intersect(List2).ToList(); Intersection.ForEach(x =&gt; { x.Property = List1.Single(y =&gt; y.ID == x.ID).Property == "1" ? "1" : List2.Single(y =&gt; y.ID == x.ID).Property == "1" ? "1" : ""; }); Result = Result.Concat(Intersection); </code></pre> <p>...but ForEach is very slow. Can someone suggest a faster way?</p>
    singulars
    1. This table or related slice is empty.
    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. 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