我在運行時爲實體框架構建表達式,並且除了從子ICollection中選擇一個屬性外,我已經解決了所有問題。Expression.Call類型System.Collections.Generic.ICollection上沒有方法'Select'
很難發佈我的整個框架,但這裏是我所嘗試過的。
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
此行引發錯誤。
Expression.Call(property, "Select", null,(MemberExpression)innerProperty);
這是錯誤。
無方法 '選擇' 存在於 型「System.Collections.Generic.ICollection`1 [ApplicationUserRole]
這就是我試圖動態地產生。
await context.Users.Where(c => c.Roles
.Select(x => x.ApplicationRole.Name)
.Contains("admin"))
.ToListAsync();
我會很感激任何人可以幫助。
選擇LAMDA表達式在System.Linq的命名空間的擴展方法,所以你不能直接調用它與反思的對象。 請參閱:http://stackoverflow.com/questions/1452261/how-do-i-invoke-an-extension-method-using-reflection –