Nhibernate Linq In Clause

二次信任 提交于 2019-12-22 02:00:08

问题


Is it possible to get Nhibernate linq to generate a query with an "In" clause? e.g. - Where AnID in (x,y,z)?


回答1:


I don't know the state of nHibernate with respect to generating all the potential LINQ queries, but you should be able to use .Contains() to generate an IN.

var list = new int[] { x, y, x };
var q = db.Entities.Where( e => list.Contains( e.AnID ) );



回答2:


Agreed, this does work. I found the generated SQL for 'not in' to be strange though (as of 3.3.0 GA)

...
from
  mytable t0_
where
  case
    when t0_.testValue in (
              @p0 , @p1 , @p2
          ) then 1
          else 0
    end=@p3
@p0 = 9 [Type: Int32 (0)],
@p1 = 99 [Type: Int32 (0)],
@p2 = 109 [Type: Int32 (0)],
@p3 = False [Type: Boolean (0)],
...

Seems a bit odd to 'case' this when 'not in' would have been clearer (not that I plan on reading every line, but in a trace/profile maybe).

(... later that day ...)

I realized that the above 'strange' choice of SQL was only when I used

.Where(e => list.Contains(e.AnID) == false)

If I used

.Where(e => !list.Contains(e.AnID))

The SQL generated is much cleaner (using 'not in')




回答3:


NHibernate has an option IsIn part of RestrictionExtensions

x => x.Name.IsIn( new[] {"a", "b"} )



来源:https://stackoverflow.com/questions/2546893/nhibernate-linq-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!