NHibernate使用QueryOver与WHERE IN

我会创build一个像这样的QueryOver

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

我已经尝试使用Contains方法,但我遇到了exception

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

在这里我的代码

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

我find了解决办法! 🙂

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

你可以尝试这样的事情:

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

在这种情况下你不需要join

这工作,更优雅

 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