1

我有以下子/标准:

var sq = DetachedCriteria.For<Title>()
  .CreateAlias("Genres", "genre")
  .Add(Restrictions.IsNull("genre.ParentId"))
  .SetProjection(Projections.Property<Genre>(g=>g.Name));

var q =
  session.CreateCriteria(typeof(Title))
      .SetProjection(
            Projections.Alias(Projections.SqlFunction("array", null, Projections.SubQuery(sq)), "TopLevelGenre"),
            Projections.Property<Title>(t1=>t1.Id)
        )
        .SetMaxResults(5);

这会导致以下 SQL 查询:

SELECT array((SELECT this_0_.title as y0_
          FROM   title this_0_
                 inner join title_genre genres3_
                   on this_0_.title_id = genres3_.title_id
                 inner join genre genre1_
                   on genres3_.genre_id = genre1_.genre_id
          WHERE  genre1_.parent_id is null)) as y0_,
   this_.title_id                             as y1_
FROM   title this_
limit  5 /* :p1 */

如何设置它以便我的子查询使用外部查询中的属性值?例如,我希望子查询按 title_id 值过滤。NHibernate 中是否有任何东西允许我将属性值投影到子查询?

谢谢!

4

1 回答 1

0

原来我很接近。我所要做的就是为 Title 创建一个别名并在我的子查询的 Where 子句中使用它。

Title title = null;

c = Session
  .QueryOver(() => title)
  ....
  .SelectList(list => list
      .Select(Projections.Alias(Projections.SqlFunction("array", null, Projections.SubQuery(sq.Where(tt => tt.Id == title.Id))), "TopLevelGenre"))
  );

希望对某人有所帮助..

于 2011-05-12T00:56:12.073 回答