2016-12-27 14 views
0

私は自分のAPPのためにMySqlデータベースでNHibernate ORMを使用しています。私は私のカテゴリのための単純なネストされたテーブルモデルを使用しています。NHibernate階層的再帰的クエリ

私のテーブルの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; 

答えて

0
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でそのクエリを行うことができます。何かを助けることができるかもしれません。

関連する問題