Reputation: 39413
I have one table "orders" with a foreing key "ProductID".
I want to show the orders in a grid with the product name, without LazyLoad for better performance, but I if use DataLoadOptions it retrieves all Product fields, which seams like a overkill.
Is there a way to retrieve only the Product name in the first query? Can I set some attribute in the DBML?
In this table says that "Foreign-key values" are "Visible" in Linq To SQL, but don't know what this means.
Edit: Changed the title, because I'm not really sure the there is no solution.
Can't believe no one has the same problem, it is a very common scenario.
Upvotes: 1
Views: 1188
Reputation: 39413
I get the solution in this other question Which .net ORM can deal with this scenario, that is related to the liammclennan answer but more clear (maybe the question was more clear too)
Upvotes: 0
Reputation: 5368
What you are asking for is a level of optimisation the linq-to-sql does not provide. I think your best bet is to create a query that returns exactly the data you want, possibly as an anonymous type:
from order in DB.GetTable<Orders>()
join product in DB.GetTable<Products>()
on order.ProductID = product.ID
select new { ID = order.ID, Name = order.Name, ProductName = product.Name };
Upvotes: 5
Reputation: 25041
If you select only the columns you want in the linq query, and then call .ToList() on the query, it will be immedietly executed, and will only bring back the columns you are interested in. For example if you do this:
var q = from p in dataContext.products select p.ProductName;
var results = q.ToList();
You will get back a list of product names stored in results, and when the query executes on the server it will only bring back the ProductName column.
Upvotes: 0