NHibernate标准用至少一个子对象查询父对象?

时间:2022-12-17 04:16:47

In my project, Lines can be grouped and a Group has a type which can be either Crossing (1) or Parallel (2). I need to find all lines which has at least one group of a specified type (in this case, 1). The Id of a given line can be either on column LineA or LineB of a group. Here is where i got so far:

在我的项目中,可以对Lines进行分组,并且Group的类型可以是Crossing(1)或Parallel(2)。我需要找到至少有一组指定类型的所有行(在本例中为1)。给定行的Id可以位于组的LineA行或LineB上。这是我到目前为止的地方:

Criteria crit = session.CreateCriteria(typeof(Line), "ln");

DetachedCriteria count = DetachedCriteria.For<Group>()
.SetProjection(Projections.CountDistinct("Id"))
.Add(Expression.Or(
    Expression.EqProperty("LineA", "ln.Id"),
    Expression.EqProperty("LineB", "ln.Id")))
.Add(Expression.Eq("GroupTypeId", 1));

crit.Add(Subqueries.Gt(0, count));

1 个解决方案

#1


0  

I got it working!

我搞定了!

crit.Add(Expression.Sql(
  "EXISTS(select 1 from Group" +
  "WHERE ({alias}.Id=LineA OR {alias}.Id=LineB)"+
    "AND GroupTypeId = ?)", (int) type, NHibernateUtil.Int32));

{alias} is a placeholder for the object being queried.

{alias}是要查询的对象的占位符。

#1


0  

I got it working!

我搞定了!

crit.Add(Expression.Sql(
  "EXISTS(select 1 from Group" +
  "WHERE ({alias}.Id=LineA OR {alias}.Id=LineB)"+
    "AND GroupTypeId = ?)", (int) type, NHibernateUtil.Int32));

{alias} is a placeholder for the object being queried.

{alias}是要查询的对象的占位符。