I have the following bit of code:
Expression<Func<Subscription, Service>> service2= subscription => (from relationship in subscription.ChildRelationships
select relationship.SecondService).FirstOrDefault();
It creates an expression that I can use later as part of a query with the entity framework. The actual code I'm using has a where clause but I've omitted it for readability. This works fine and I'm able to run it in LINQPad which I'm using for testing.
If I change the code to:
Expression<Func<Subscription, IQueryable<Service>>> service2= subscription => (from relationship in subscription.ChildRelationships
select relationship.SecondService);
It no longer compiles and has the following error:
Cannot convert lambda expression to delegate type 'System.Func<Farmworks.Data.Subscription,System.Linq.IQueryable<Farmworks.Data.Service>>' because some of the return types in the block are not implicitly convertible to the delegate return type
It seems to be because ChildRelationships which is a navigation property doesn't implement IQueryable. It is actually of type EntityCollection which does implement IEnumerable but is no good for creating expressions that work with EF.
I think I understand why the second block of code doesn't work and would like to know how to rewrite it so that it does. What also puzzles me is why the first block of code does work. It also uses the ChildRelationships navigation property but doesn't have any problem becoming an expression that works with EF.
Can anyone shed any light?