简体   繁体   English

NHibernate分层递归查询

[英]NHibernate hierarchical recursive query

i am using NHibernate ORM with MySql database for my APP. 我为我的APP使用NHibernate ORM和MySql数据库。 i am using simple nested table model for my categories. 我正在为我的类别使用简单的嵌套表模型。

my table sql : 我的表sql:

CREATE TABLE `cat` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `catId` int(11) DEFAULT '0',
  `dr` bit(1) DEFAULT NULL,
  `st` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

my category class: 我的类别课程:

   public class cat
    {
        [Key]
        public virtual int id { get; set; }
        public virtual int catId { get; set; }
        public virtual bool dr { get; set; }
        public virtual DateTime st { get; set; }

        [ForeignKey("catId")]
        public virtual IList<cat> cats { get; set; }
    }

How can i do this query with NH : 我如何使用NH进行此查询:

select t2.* from (select id from cat where catId=0 and dr=1) t1 join 
cat t2 On(t1.id=t2.catId) where t2.st<Now() and t2.dr=1;
    using NHibernate;
    using NHibernate.Criterion;
    using NHibernate.Linq;
    using NHibernate.Transform;
    using System;
    using System.Collections.Generic;
    using System.Linq;

.......... ..........

            var q2 = db.Query<cat>()
                .Where(x => x.catId== 0 && x.dr == true);

            q2 = from t1 in q2
                 join t2 in db.Query<cat>() on t1.id equals t2.catId
                 where t2.st< DateTime.Now && t2.dr== true
                 select t2;

            //for result list
            var myList = q2.ToList();

i can do that query with NHibernate & Linq. 我可以使用NHibernate和Linq进行查询。 May be it can be help some one. 可能对某些人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM