I am building expressions for entity framework at run time and I have solved all problems except selecting a property from a child ICollection.
It is difficult to post my whole framework but here is what I have tried.
var param = Expression.Parameter(typeof(TEntity), "w");
Expression.Property(entity, propertyName);
w.Roles
var param = Expression.Parameter(typeof(TChild), "z");
Expression.Property(entity, propertyName);
z.ApplicationRole.Name
This line throws the error.
Expression.Call(property, "Select", null,(MemberExpression)innerProperty);
This is the error.
No method 'Select' exists on type 'System.Collections.Generic.ICollection`1[ApplicationUserRole]
This is what I am trying to produce dynamically.
await context.Users.Where(c => c.Roles
.Select(x => x.ApplicationRole.Name)
.Contains("admin"))
.ToListAsync();
I'd appreciate anyone who could help.
As already mentioned in comment,
Select
is not a method of IColletion, it's an extension method and you couldn't invokeSelect
directly fromICollection
.You could create
Select
MethodInfo
this way:and your Expression you could create as:
Important is, that first parameter of
Expression.Call
is null,Select
is a static extension method and it don't have any instance to be called on.lambda
hier is a lamda expression from your property Expression