Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One possibly quite easy way of achieving this is to provide a method that takes a function to retrieve the item to compare. Something like:</p> <pre><code>private List&lt;T&gt; Reduce&lt;T, TProp&gt;(List&lt;T&gt; list, Func&lt;T, TProp&gt; getId) { } </code></pre> <p><sub>I have deliberately not provided an implementation for <code>Reduce</code> because I cannot head-compile the dependencies of things like <code>Contains</code>, which often need items that implement interfaces.</sub></p> <p>This will allow you to supply different classes and use the <code>Func</code> to generalise the retrieval of the ID:</p> <pre><code>var customers = new List&lt;Customer&gt;(); customers = Reduce&lt;Customer, Guid&gt;(customers, c =&gt; c.CustomerId); var products = new List&lt;Product&gt;(); products = Reduce&lt;Product, int&gt;(products, p =&gt; p.ProductId); </code></pre> <p>The support for type inference in the compiler will often let you get away with just:</p> <pre><code>customers = Reduce(customers, c =&gt; c.CustomerId); products = Reduce(products, p =&gt; p.ProductId); </code></pre> <p>You may have to constrain <code>TProp</code> with <code>IEquatable</code> or something in order to get it to work with a <code>Contains</code> implementation, but this is the route I would explore first:</p> <pre><code>private List&lt;T&gt; Reduce&lt;T, TProp&gt;(...) where TProp : IEquatable&lt;TProp&gt; </code></pre> <p><hr/> An alternative is to generalise the functionality over the classes using an interface that exposes the ID. In the face of <code>Func</code> I now tend to err away from this route if I am making the changes retroactively.</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