Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>rich.okelly and Ladislav Mrnka are both correct in different ways.</p> <p>Both their answers deal with the fact that the <code>IEqualityComparer&lt;T&gt;</code>'s methods won't be translated to SQL.</p> <p>I think it's worth looking at the pros and cons of each, which will take a bit more than a comment.</p> <p>rich's approach re-writes the query to a different query with the same ultimate result. Their code should result in more or less how you would efficiently do this with hand-coded SQL.</p> <p>Ladislav's pulls it out of the database at the point before the distinct, and then an in-memory approach will work.</p> <p>Since the database is great at doing the sort of grouping and filtering rich's depends upon, it will likely be the most performant in this case. You could though find that the complexity of what's going on prior to this grouping is such that Linq-to-entities doesn't nicely generate a single query but rather produces a bunch of queries and then does some of the work in-memory, which could be pretty nasty.</p> <p>Generally grouping is more expensive than distinct on in-memory cases (especially if you bring it into memory with <code>AsList()</code> rather than <code>AsEnumerable()</code>). So if either you were already going to bring it into memory at this stage due to some other requirement, it would be more performant.</p> <p>It would also be the only choice if your equality definition was something that didn't relate well to what is available just in the database, and of course it allows you to switch equality definitions if you wanted to do so based on an <code>IEqualityComparer&lt;T&gt;</code> passed as a parameter.</p> <p>In all, rich's is the answer I'd say would be most-likely to be the best choice here, but the different pros and cons to Ladislav's compared to rich's makes it also well worth studying and considering.</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. 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.
    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