0

私はNHibernateに非常に慣れていません。これは CreateSQLQuery を使用して作成しましたが、可能であれば NHibernate Criteria 形式に移行したいと考えています。私のクエリは次のようになります。

select parent.ID as Id, ValueA.Total as ValueACount, ValueB.Total as ValueBCount
from ParentTable parent 
left outer join
(
  select count(*) as Total, ID 
  from ChildTable
  where state = 'ValueA' 
  group by ID
) ValueA on ValueA.ID = parent.ID
left outer join
(
  select count(*) as Total, ID
  from ChildTable
  where state = 'ValueB' 
  group by ID
) ValueB on ValueB.ID = parent.ID

テーブルの名前/値を変更して、少し抽象化しました。コードはそのまま機能しますが、これがソリューション全体で唯一のクエリです。私たちがそれを取り除くことができるかどうか見てみたい.

助けていただけるすべての人に前もって感謝します。私を助けることができる本当に良いウェブページへのリンクを私に与えたいのであれば、それも結構です. 私は少なくともあなたを高く評価します:)

私も似たような質問を見たことがあります。他の質問/回答が非常に役立つと思われる場合は、お気軽にそれを指摘してください。

4

1 に答える 1

2

linqfuを試すことができます

var results = from p in session.Query<Parent>()
              select new
              {
                  p.Id,
                  ValueACount = (from c1 in session.Query<Child>() where c1.State == "ValueA" && c1.Parent == p select c1).Count(),
                  ValueBCount = (from c2 in session.Query<Child>() where c2.State == "ValueB" && c2.Parent == p select c2).Count(),
              };

または基準を使用する

var results = session.CreateCriteria<Parent>("p")
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Id"))
        .Add(Projections.SubQuery(DetachedCriteria.For<Child>()
            .Add(Restrictions.Eq("State", "ValueA") && Restrictions.EqProperty("Parent", "p"))
            .SetProjection(Projections.RowCount())))
        .Add(Projections.SubQuery(DetachedCriteria.For<Child>()
            .Add(Restrictions.Eq("State", "ValueB") && Restrictions.EqProperty("Parent", "p"))
            .SetProjection(Projections.RowCount()))))
    .List();
于 2012-04-18T05:52:40.410 に答える