我可以在什么 class 上找到 EF Core 5.0 的 DbSet<TEntity> 类型的扩展 'Where'?
On what class can I find the extension 'Where' for the type DbSet<TEntity> of EF Core 5.0?
我正在编写一种方法,该方法使用 Linq 表达式生成一些 EFCore Linq 代码以进行特定过滤 API。它会写一些像这样的语句:
dbContext.Student
.Where(s => s.Address.ZipCode == 10005)
.Intersect(dbContext.Student
.Where(s => s.FirstName == "John")
为此,我需要获取 Where 和 Intersect 方法的 MethodInfo。
我尝试在类型上使用 GetMethod,但它 returns 为空(不适用于扩展方法):
MethodInfo method = typeof(Queryable).GetMethod("Where",
BindingFlags.Public | BindingFlags.Static,
null,
CallingConventions.Any,
new[] { typeof(IQueryable<Student>),
typeof(Expression<Func<Student, bool>>)},
null);
我还尝试了以下方法:
MethodInfo method = typeof(Queryable)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(mi => mi.Name == "Where");
// TO DO : taking the first of where Methods is bad.
.First()
.MakeGenericMethod(typeof(DbSet<Student>));
但是在 DbSet 上使用时我得到一个 badArgument0。
关于 class 我在哪里可以找到正确的 Where DbSet 扩展的任何线索?
这里不需要MethodInfo
,你可以只创建调用表达式:
var queryable = dbContext.Student.AsQueryable();
var lambda = ...
var whereCall = Expression.Call(typeof(Queryable),
nameof(Queryable.Where),
new[] { typeof(Student) },
queryable.Expression,
lambda
);
// and final
return queryable.Provider.CreateQuery<Student>(whereCall);
我正在编写一种方法,该方法使用 Linq 表达式生成一些 EFCore Linq 代码以进行特定过滤 API。它会写一些像这样的语句:
dbContext.Student
.Where(s => s.Address.ZipCode == 10005)
.Intersect(dbContext.Student
.Where(s => s.FirstName == "John")
为此,我需要获取 Where 和 Intersect 方法的 MethodInfo。
我尝试在类型上使用 GetMethod,但它 returns 为空(不适用于扩展方法):
MethodInfo method = typeof(Queryable).GetMethod("Where",
BindingFlags.Public | BindingFlags.Static,
null,
CallingConventions.Any,
new[] { typeof(IQueryable<Student>),
typeof(Expression<Func<Student, bool>>)},
null);
我还尝试了以下方法:
MethodInfo method = typeof(Queryable)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(mi => mi.Name == "Where");
// TO DO : taking the first of where Methods is bad.
.First()
.MakeGenericMethod(typeof(DbSet<Student>));
但是在 DbSet 上使用时我得到一个 badArgument0。
关于 class 我在哪里可以找到正确的 Where DbSet 扩展的任何线索?
这里不需要MethodInfo
,你可以只创建调用表达式:
var queryable = dbContext.Student.AsQueryable();
var lambda = ...
var whereCall = Expression.Call(typeof(Queryable),
nameof(Queryable.Where),
new[] { typeof(Student) },
queryable.Expression,
lambda
);
// and final
return queryable.Provider.CreateQuery<Student>(whereCall);