English 中文(简体)
草场工程开始在NHibernate进行复杂的预测
原标题:Getting started with complex projection in nHibernate

我正在研究尽可能采用“自由实体框架”制度。 我正试图以同样方式使用“自由”来做如下工作:

SELECT
   a.Id,
   a.Name,
   a.Etc,
   MAX(CASE WHEN (c.dId IS NOT NULL) THEN 1 ELSE 0 END) As IsLinked
FROM
   tA a
   INNER JOIN tAB ab ON a.Id = ab.aId
   INNER JOIN tB b ON ab.bId = b.Id
   LEFT OUTER JOIN tC c ON b.cId = c.Id
   AND c.dId =  x 
WHERE
   a.Id =  y 
GROUP BY
   a.Id,
   a.Name,
   a.Etc,

基本上,我想从表tA中挑选,并检查数字A的条目是否与表tC中的数据有关。 两个表格之间的联系很复杂:

  • tA has a many to many relationship to tB (using table tAB)
  • tB has a one to many relationship with the table tC using the Foreign Key tB.cId
  • tC has a Foreign Key tc.dId to another table tD

采用实体框架法 我可以不使用,因为它不允许过滤,但我可以使用预测,使我能够使用功能上正确的询问:

var query = from a in db.As
            where a.Id ==  y 
            select new
            {
               a,
               IsLinked = a.Bs
                           .Select(b => b.Cs)
                           .Select(c => c.Where(n => n.dId ==  x ))
                           .Select(h => h.Count()).Max() > 0
             };

由此产生的结构复杂而缓慢,但行之有效。 从QueryOver开始,我努力在自由邦实现同样的功能:

var query = session.QueryOver<A>().Where(a => a.Id ==  y );

How do I create an equivalent projection that works across the multiple / nested join types (one being an OUTER join) without resorting to HQL? Or am I best off (since performance is likely to be poor) either:

  • Making two queries (one that retrieves the data and one that establishes the linkage)
  • Introducing views that encapsulate the complexity of the joins

增 编

最佳回答

问题应当等同,你能否尝试?

var query = from a in db.As
        where a.Id ==  y 
        select new
        {
           a,
           IsLinked = a.Bs.SelectMany(b => b.Cs).Any(c => c.dId ==  x )
           // or
           IsLinked = a.Bs.Any(b => b.Cs.Any(c => c.dId ==  x ))
         };
问题回答

暂无回答




相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签