I am creating a LINQ query dynamically, and so far everything is fine.
But I got stuck where I thought I wouldnβt. At some point when creating this request, I need to access the EntityCollection element of an object. Something like that:
Expression collection = Expression.Property(entity, typeof(EntityType).GetProperty("CollectionOfRelatedEntities"));
Then I would call the LINQ Where method for this collection:
MethodCallExpression AfterWhere = Expression.Call( typeof(Queryable), "Where", new Type[] { typeof(RelatedEntity) }, collection, Expression.Lambda<Func<RelatedEntity, bool>>(predicate, new ParameterExpression[] { paramOfRelatedEntity }));
And usually it will work. In this case, this will not happen because the collection is IEnumerable, and I need it to be IQueryable in order for "Where" to work.
I tried this:
Expression.Convert(collection, typeof(IQueryable<RelatedEntity>);
but he says he cannot be started because EntityCollection does not implement IQueryable.
I statically use AsQueryable to achieve what I need here, so I tried to simulate this dynamically:
Expression.Call(collection, typeof(EntityCollection<RelatedEntity>).GetMethod("AsQueryable"));
but I get an exception to exclude links. I cannot achieve this through reflection. This AsQueryable method is an extension method, it is static defined in the Queryable class, so I tried:
Expression.Call(collection, typeof(Queryable).GetMethod("AsQueryable", BindingFlags.Static));
Same result: "Value cannot be null."
I am reaching my limits here, and I am fresh from ideas.
So I ask you:
How can I dynamically port IEnumerable to IQueryable?