When I do:
var q = from c in metaData.Customer
orderby c.Country ascending
select new
{
c.CustomerId,
c.CompanyName,
c.Country,
Name = c.EmployeeCollectionViaOrder.First().FirstName + c.EmployeeCollectionViaOrder.First().LastName
};
I get an error that the function call of '+' can't contain queries. This is because the c.EmployeeCollectionViaOrder.First() call is a separate query.
When I do:
var q = from c in metaData.Customer
orderby c.Country ascending
select new
{
c.CustomerId,
c.CompanyName,
c.Country,
FirstName = c.EmployeeCollectionViaOrder.First().FirstName,
LastName = c.EmployeeCollectionViaOrder.First().LastName
};
it works, though I have to concat the names myself in memory.
When I move the firstname and lastname fetches to let statements AFTER the orderby, I get a problem in the SQL with an unaliased usage of a table:
var q = from c in metaData.Customer
orderby c.Country ascending
let firstName = c.EmployeeCollectionViaOrder.First().FirstName
let lastName = c.EmployeeCollectionViaOrder.First().LastName
select new
{
c.CustomerId,
c.CompanyName,
c.Country,
Name = firstName + lastName
};
It's also not that efficient with all the nesting of the scalars, however this is how let works: it basicly ruins your query's performance.
I could also try the c.EmployeeCollectionViaOrder.First() call in the projection:
var q = from c in metaData.Customer
orderby c.Country ascending
select new
{
c.CustomerId,
c.CompanyName,
c.Country,
Employee = c.EmployeeCollectionViaOrder.First()
};
but this gives a problem too: the related entity is m:n related, over 'order' which isn't part of the in-memory set to merge with. This means that I get two sets: one with CustomerId and one with EmployeeId, which gives a problem: there's no way to match the two.
I'll look into the problem with the let statements.