[英]How to map Expression<Func<TEntity, bool>> to Expression<Func<TDbEntity, bool>>
[英]Apply Expression<Func<TEntity, bool>> in Generic Repository?
目前,我有一个通用存储库,如下所示:
public virtual IQueryFluent<TEntity> Query(Expression<Func<TEntity, bool>> query)
{
return new QueryFluent<TEntity>(this, query);
}
并像这样实现:
var obj = _repository.Query(x => x.RoleID == ID).Select().FirstOrDefault();
因为RoleID出现在所有实体中,所以我想像这样在通用存储库中构建一个通用的表达式:
//Check and get RoleID if exist this column
PropertyInfo info = typeof(TEntity).GetProperty("RoleID");
if (info != null)
Expression<Func<TEntity, bool>> filter = (p => p.RoleID ??? or p.info ??? == 5);
我们如何建立通用存储库中TEntity的未知列过滤器? 还是可以为Filter传递一串where子句?
我的目的是,我只需要具有以下简单结构的查询数据:
var obj = _repository.Query().Select().FirstOrDefault();
请指教。
谢谢。
您可以使用界面使用RoleID修改这些实体,例如:
interface IEntity { }
interface IRoleIdentifiedEntity : IEntity
{
int RoleID { get; }
}
对于所有实体,请使用通用存储库:
class GenericRepository<T> where T : IEntity
{
public IEnumerable<T> Query(Expression<Func<T, bool>> predicate)
{
throw new NotImplementedException();
}
}
对于具有IRoleIdentifiedEntity
实体,请使用另一个存储库:
class RoleIdentifiedRepositoryBase<T> : GenericRepository<T>
where T : IRoleIdentifiedEntity
{
public IEnumerable<T> Query(int id)
{
return Query(e => e.RoleID == id);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.