【问题标题】:NHibernate hierarchical recursive queryNHibernate分层递归查询
【发布时间】:2017-05-11 23:24:18
【问题描述】:

我正在为我的 APP 使用 NHibernate ORM 和 MySql 数据库。我正在为我的类别使用简单的嵌套表模型。

我的表 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;

我的分类类:

   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; }
    }

我如何使用 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;

【问题讨论】:

    标签: c# mysql sql nhibernate orm


    【解决方案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();
    

    我可以使用 NHibernate 和 Linq 进行该查询。也许它可以帮助一些人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2014-10-27
      相关资源
      最近更新 更多