views:

801

answers:

3

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.

A: 

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.

Scott Wisniewski
Do you mean having a list a search that list for the name for every record?
Eduardo Molteni
No...Normally, with linq, when you run "var q = from c in db.Custs select c;"the query doesn't get executed until you foreach over q. It also brings down all customer fields.If you run q.ToList(), the "ToList" method will foreach over q and insert the results in the list.
Scott Wisniewski
Also... if you change the fields in the query, so that you do"var q = (from c in db.Custs select c.CustID, c.CustName).ToList();"then the database will:1) Execute the query immedietly2) Only bring down the CustID and CustName columns.
Scott Wisniewski
But I want to bring all the order fields, plus the name of the product (and the name of the customer, etc). How this related to the question?
Eduardo Molteni
You said that:1) You don't want to use Lazy Loading2) You have a set of specific columns you want to bring down, and you don't want anything other than those columns brought downRunning ToList() runs the query up frontchanging the select list brings down exactly the set of fields you ask for.
Scott Wisniewski
+5  A: 

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 };
liammclennan
Living with all this anonymous types will be a nightmare.
Eduardo Molteni
@Eduardo: On the contrary, in some cases (this type of case in particular) anonymous types can actually make things a lot simpler.
BenAlabaster
A: 

I get the solution in this other question http://stackoverflow.com/questions/381049/which-net-orm-can-deal-with-this-scenario#381084, that is related to the liammclennan answer but more clear (maybe the question was more clear too)

Eduardo Molteni