Is it possible to return multiple entities from a LinqToSql (or EF) query (the query is inside a method) so that the results will still be composable ?
Something like this:
public IQueryable<KeyValuePair<Customer, Product>> GetCustomerEntities()
{
return
(
from customer in this.Context.Customers
join
product in this.Context.Products on customer.ID equals product.CustomerID
select new KeyValuePair<Customer, Product>(customer, product)
);
}
Then I want to use the result of this method to further compose the query like:
this.GetCustomerEntities().Where(e => e.Key.Name == "my customer")
The method above compiles but it can’t be executed by LinqToSql because it can’t convert KeyValuePair to SQL, which is the expected behavior.
Is it possible to achieve this somehow ?
The problem here is that the runtime can’t see that passing
customerinto the constructor parameter is the same thing as looking ate.Key, since those two things are not obviously the same. You might try creating your own POCO type with getter and setter, i.e.and using that instead of
KeyValuePair<Customer,Product>.Inside a single method, an anonymous type would be the obvious choice.