Edit : the preceding explanation is wrong since 'it works on my machine' :
var contract = dataContext.Contracts.First();
var room = dataContext.Rooms.First(r => r.Contract == contract);
something must be broken in your association between your classes.
You can get the executed command by using
var command = dataContext.GetCommand(
dataContext.Rooms.Where(r => r.Contract == contract))
.CommandText;
The resulting command text is :
SELECT ... FROM [Rooms] AS [t0]
WHERE [t0].[ContractId] = @p0
So we can see that the primary key is found from the entity type...
Isn't the first version supposed to
work? The Employee entity is from the
same datacontext...
Yes but the lambda expression is converted to SQL script and it seems that the linq Sql script builder doesn't try to find the entity key to generate the request.
The linq to Sql command builder use the lambda content as an Expression<> tree to build the query text. To perform this operation it expect the lambda to contain expressions referencing entities properties.
The problem here seems to be that it references the entities themselve and not there properties.
The following query
var something = someEntity.SomeThings
.First(t => t.IdEmployee == employee.IdEmployee);
produces Sql that will be something like
SELECT ... FROM SomThings
WHERE IdEmployee = @idEmployee
The table columns are found using the properties names in the lambda.
In the other case, there is no property name...