Run it through LINQPad… This won’t work as you’d …

Comment posted on How To: Subqueries In LINQ to SQL by Craig L. Finck

Run it through LINQPad…

This won’t work as you’d hope.

First, it’ll select all of the columns from the tables in your primary query (rather than just the columns you need to shape your result object). Then, it’ll fire the subquery (which now resides inside its own function) once for each result from the primary query. Decidely not the desirable result.

Recent comments by Craig L. Finck

  • LINQ Distinct With Anonymous Types Vs. Explicit Types
    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.

powered by SEO Super Comments

Rss Feed