1

我想知道是否有一个转换器,我可以在其中键入 SQL 语句并接收其流畅的 Nhibernate 等效项?

我有这个 SQL 语句:

 SELECT F.* 
 From Foo F
      INNER JOIN HamSandwich HS
           ON F.HamSandwichId = HS.HamSandwichId
 Where 
      HS.MustardId = 
           (Select MustardId
            From HamSandwich
            Where HamSandwichId = 1)   

我想把它变成一个流畅的 Nhibernate 查询。

session.QueryOver<Foo>()
       .JoinQueryOver(foo => foo.HamSandwich)
       .Where() // this is where I am still drawing a blank
4

1 回答 1

2

假设有关您的实体和映射的一些事情,这是等效的 QueryOver:

IList<Foo> foos = session.QueryOver<Foo>()
    .JoinQueryOver(foo => foo.HamSandwich)
    .WithSubquery.WhereProperty(hs => hs.Mustard.Id).Eq(
        QueryOver.Of<HamSandwich>()
            .Where(hs => hs.Id == 1)
            .Select(hs => hs.Mustard.Id))
    .TransformUsing(Transformers.DistinctRootEntity)
    .List<Foo>();

这会生成以下 SQL:

SELECT this_.*
FROM   [Foo] this_
       inner join [HamSandwich] hs1_
         on this_.HamSandwichId = hs1_.Id
WHERE  hs1_.MustardId = (SELECT this_0_.MustardId as y0_
                                   FROM   [HamSandwich] this_0_
                                   WHERE  this_0_.Id = 1 /* @p0 */)
于 2013-08-29T14:47:38.653 回答