【问题标题】:NHibernate QueryOver with RestrictionsNHibernate QueryOver 有限制
【发布时间】:2014-07-07 03:03:14
【问题描述】:

我是 NHibernate 的新手,正在尝试使用 QueryOver。我有以下 NHibernate 查询

var departments = session
   .QueryOver<Department>()
   .Where(Restrictions.On<Department>x=> x.Parent.Id).IsIn(new List<int> {100}))
   .List().ToList();

我有一个值要传递给 IsIn,为此我必须新建一个列表 (new List&lt;int&gt; {100}))。有没有更清洁的方法?

【问题讨论】:

    标签: c# nhibernate linq-to-nhibernate queryover


    【解决方案1】:

    你的语法没问题,只有很少的更改或改进可以做......他们是:

    IList<Department> departments;
    var parents = new List<int> {167};
    
    // advantage of this "original" QueryOver is, that it can accept
    // more parent IDs.. not only one "100" as in our example
    // so if we neet children of 100,101,102
    // we can get more from this syntax: new List<int> {100, 101,102...};
    departments = session
        .QueryOver<Department>()
        .Where(Restrictions.On<Department>( x=> x.Parent.Id)
            .IsIn(parents))
        .List();
    
    // this style is just a bit more straightforward 
    // saving few chars of code, using 'WhereRestrictionOn'
    departments = session
        .QueryOver<Department>()
        .WhereRestrictionOn(x => x.Parent.Id).IsIn(parents)
        .List();
    
    // in case we do have the only one parent ID to search for
    // we do not have to use the IS IN
    departments = session
        .QueryOver<Department>()
        .Where(x => x.Parent.Id == 100)
        .List();
    

    查看更多:16.2. Simple Expressions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多