使用带有WHERE IN的QueryOver的NHibernate

时间:2011-03-23 16:49:20

标签: c# nhibernate queryover where-in

我会像这样创建一个QueryOver

SELECT *
FROM Table
WHERE Field IN (1,2,3,4,5)

我尝试使用Contains方法,但我遇到了异常

  

“System.Exception:无法识别的方法调用:System.String:Boolean包含(System.String)”

这是我的代码

var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)                                                                
  .JoinAlias(() => baseModel.Submodels, () => subModels)
  .Where(() => subModels.ID.Contains(IDsSubModels))
  .List<MyModel>();

3 个答案:

答案 0 :(得分:60)

我找到了解决方案!! : - )

var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)
    .JoinAlias(() => baseModel.Submodels, () => subModels)
    .WhereRestrictionOn(() => subModels.ID).IsIn(IDsSubModels)
    .List<MyModel>();

答案 1 :(得分:47)

您可以尝试这样的事情:

// if IDsSubModels - array of IDs
var qOver = _HibSession.QueryOver<MyModel>() 
                       .Where(x => x.ID.IsIn(IDsSubModels))

在这种情况下你不需要加入

答案 2 :(得分:11)

这很有效,而且更优雅

var Strings = new List<string> { "string1", "string2" };

var value = _currentSession
.QueryOver<T>()
.Where(x => x.TProperty == value)
.And(Restrictions.On<T>(y=>y.TProperty).IsIn(Strings))
.OrderBy(x => x.TProperty).Desc.SingleOrDefault();

where T is a Class and TProperty is a property of T