Craig, this is a great tip/idea/insight. So would …

Comment posted on LINQ Distinct With Anonymous Types Vs. Explicit Types by Yogesh Ramakrishnan

Craig, this is a great tip/idea/insight.

So would you suggest using the first approach or would you suggest imlementing an equality operator?

BTW, thanks for sharing this, keep these coming.
-Yogesh

Recent comments by Yogesh Ramakrishnan

  • How To: Subqueries In LINQ to SQL
    I try to extract the sub query to its own method to make it easy to read. Do you see any issues with that?
    As always I find this blog very informative.

powered by SEO Super Comments



2 Responses to “Craig, this is a great tip/idea/insight. So would …”

  1. Craig, this is a great tip/idea/insight.

    So would you suggest using the first approach or would you suggest imlementing an equality operator?

    BTW, thanks for sharing this, keep these coming.
    -Yogesh

  2. Thank you, Yogesh.

    This depends.

    I like when I can have the database perform functions that it is good at performing, like a distinct operation. It also means that I don’t need to send a bunch of results back over the wire because the server will narrow those down for me.

    The explicitly defined type is scary because a) it won’t perform the distinct operation on the server and b) it could easily return unexpected duplicates.

    However, I have to weigh whether or not an anonymous type makes sense for the return object. Is that object going to be reusable elsewhere, selectable by other queries, etc.? And, does the distinct operation that is needed really define equality for the object?

    There are other options for solving the problem. Among them are defining your own IEqualityComparer to pass to the Distinct function, and writing a stored procedure to select the specific results required. Most frequently, on my current project, I’d be using the anonymous type.

Rss Feed