When I used to use ADO.NET, I used to create TableAdapters which were populated using joined queries, so for example the product table adapter would have column for ‘location’ even if the underlying select query joined the product to the location table.
In LINQ, how do I accomplish this? Do I return the columns in the joined query by writing LINQ queries in a ProductsDao class? What type am I returning for binding to the gridview? The problem with retuning the LINQ results back is that I would have thought it would break the consistency of the ProductsDao class (this should only return product rows from the db).
I think the best option is to define classes that represent the joined data you want to return (e.g.
ProductDetails) and then returnIQueryable<ProductDetails>from your data layer.To return queryable, you can write just a simple LINQ query:
This approach has several benefits:
You’re not exposing the
YourDataContextobject directly – it can stay as a private field of the DAO class (if it is short lived) or you can created it as you need it.You’re not exposing the underlying LINQ entities directly (e.g.
ProductorDetails), so the users cannot accidentally mess with your database (unless you give them a way to do that)You’re returning queryable, which just represents a query. When the users add their additional constrains (e.g.
where), the query will be composed and you won’t be loading unnecessary data.You can expose only reasonable functionality from the class – for example, if you have only method
GetProductstakingcategoryID, your users can only fetch products in a given category (which makes it impossible to fetch the entire table from the database)