【发布时间】:2018-04-16 10:14:23
【问题描述】:
我正在尝试使用 Fluent NHibernate 编写动态查询,并努力弄清楚如何应用动态 where 条件。
这是一个 ASP.NET MVC5 项目,我希望在视图中显示一个 DTO 表,让用户能够对任何行应用过滤器。出于性能原因,必须将过滤器传递给数据库。
假设我有以下类定义:
// Entity in database
public class EntityA
{
public int Id {get; set;}
public string Name {get; set;}
public IEnumerable<EntityB> Children {get; set;}
}
// Entity in database
public class EntityB
{
public int Id {get; set;}
public string Name {get; set;}
public EntityA Parent {get; set;}
}
// DTO that is displayed in view / filter criteria is based on
public class Dto
{
public int Id {get; set;}
public string AName {get; set;}
public string BName {get; set;}
}
我在IEnumerable<FilterProperty> 中收到用户的过滤条件,如下所示:
public class FilterProperty
{
public string Name {get; set;}
public dynamic Value {get; set;}
}
我的 NHibernate 查询如下:
EntityA aliasA = null;
EntityB aliasB = null;
Dto aliasDto = null;
var query = QueryOver.Of(() => aliasB)
.JoinAlias(() => aliasA.Parent, () => aliasA)
.SelectList(l => l
.Select(() => aliasB.Id).WithAlias(() => aliasDto.Id)
.Select(() => aliasA.Name).WithAlias(() => aliasDto.AName)
.Select(() => aliasB.Name).WithAlias(() => aliasDto.BName)
)
.TransformUsing(Transformers.AliasToBean<Dto>());
我感到困惑的是,如何将针对 DTO 的过滤器的平面列表转换为针对可以传递给 .Where() NHibernate 方法的实体对象的表达式树? p>
是否可以在转换后应用限制条件?
【问题讨论】:
-
您的过滤器应该如何连接?使用
&&或||? -
所有过滤器将
&&在同一级别。
标签: c# nhibernate fluent-nhibernate queryover