Linq to Entities (EF): How to get the value of a FK without doing the join

Posted by Chu on Stack Overflow See other posts from Stack Overflow or by Chu
Published on 2009-04-22T20:55:39Z Indexed on 2010/06/11 17:33 UTC
Read the original article Hit count: 162

Filed under:
|
|

I'm using the Linq to Entities. I've got my main table, Employee setup with a field named vendorID. Vendor ID is a foreign key into the Vendors table.

As it is right now, the Employee object does not directly expose the vendorID. Instead, I can only access it this way:

var employee = (from e in context.Employees.Include("tbl_vendors")
               where e.employeeID = 1
               select e).FirstOrDefault();

//this gets the vendor ID
int vendorID = employee.tbl_vendors.vendorID;

That is just fine and dandy, but it is extra work on the database because it is forcing a join where none is needed. Is there a way to get that key value without being forced to do a join to the tbl_vendors table?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET