Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 使用动态构建的表达式过滤非泛型DbSet_Entity Framework_Linq_Generics_Expression Trees_Dbset - Fatal编程技术网

Entity framework 使用动态构建的表达式过滤非泛型DbSet

Entity framework 使用动态构建的表达式过滤非泛型DbSet,entity-framework,linq,generics,expression-trees,dbset,Entity Framework,Linq,Generics,Expression Trees,Dbset,绘图: 我有一个类作为实体框架DB上下文的外观实现。它是为了保持向后兼容性而开发的,它模仿具有相同公共接口的类,但使用DTO而不是EF实体 问题: 我在上面描述的类中有下一个方法。见下面的代码: public IQueryable<T> FindBy<T>(Expression<Func<T, Boolean>> predicate) where T : BaseDto { //GetDestinationType takes source ty

绘图:

我有一个类作为实体框架DB上下文的外观实现。它是为了保持向后兼容性而开发的,它模仿具有相同公共接口的类,但使用DTO而不是EF实体

问题:

我在上面描述的类中有下一个方法。见下面的代码:

public IQueryable<T> FindBy<T>(Expression<Func<T, Boolean>> predicate) where T : BaseDto {

//GetDestinationType takes source type of some declared mapping and returns destination type
var entityType = Mapping.Mapper.GetDestinationType(typeof (T));

var lambda = Expression.Lambda(predicate.Body, Expression.Parameter(entityType));

// dbContext declared as class field and initialized in constructor
var query = dbContext.Set(entityType).Where(lambda); // <-- Cannot use non-generic expression/lambda

return query.ProjectTo<T>(mapper.ConfigurationProvider); }
public IQueryable FindBy(表达式谓词),其中T:BaseDto{
//GetDestinationType接受某个已声明映射的源类型并返回目标类型
var entityType=Mapping.Mapper.GetDestinationType(typeof(T));
var lambda=Expression.lambda(predicate.Body,Expression.Parameter(entityType));
//dbContext声明为类字段并在构造函数中初始化

var query=dbContext.Set(entityType).Where(lambda);//问题已经解决。解决方案非常明显。而不是

var query = dbContext.Set(entityType).Where(lambda);
我会写字

var query = dbContext
                .Set(entityType)
                .ProjectTo<T>(mapper.ConfigurationProvider)
                .Where(predicate);
var query=dbContext
.Set(entityType)
.ProjectTo(mapper.ConfigurationProvider)
.Where(谓语);
其中,谓词是FindBy()方法的输入参数

这段代码将被成功编译,更重要的是,EF将构建对数据库的最佳查询,该查询将在查询体中包含Where()子句,因此它不会从数据库端获取完整的记录集