使用Criteria API在NHibernate中对此LINQ to SQL查询的正确翻译是什么?
var result = from e in Employees
where e.WorkTimeEntries.Any(t => t.DateTime >= new DateTime(2012, 3, 1))
select e;
我试过了:
var employees = Session.QueryOver<Employee>();
var timeWorkedAlias = new HashedSet<WorkTimeEntry>();
var timeWorked = employees.Left.JoinQueryOver(e => e.WorkTimes,
() => timeWorkedAlias);
timeWorkedAlias.Where (wa => wa.DateTime >= new DateTime(2012,3,1));
// How do to include the timeWorkedAlias 'filter' in the query?
var result = employees.List();
我缺少几个步骤来包含要包含在查询中的日期过滤器。
答案 0 :(得分:3)
我现在无法测试,但应该工作:
var result = Session.QueryOver<Employee>()
.JoinQueryOver(x => x.WorkTimes)
.Where(wt=> wt.DateTime >= new DateTime(2012, 3, 1))
.List();
它应该根据子集合的属性过滤父实体(Employee)(因为它是内连接)。但是,我还没有测试过它。
答案 1 :(得分:1)
我想我找到了答案(在安德烈的帮助下)
这似乎有效:
var result = Session.QueryOver<Employee>()
.JoinQueryOver<WorkTimeEntry>(e => e.WorkTimes)
.Where(t => t.DateTime > new DateTime(2012,3,1))
.List();
诀窍是指定JoinQueryOver<WorkTimeEntry>
注意,JoinQueryOver的重载需要一个IEnumerable,而且 C#编译器从中推断出类型。如果您的收藏类型不是 IEnumerable,然后您需要限定子标准的类型: