54

I would create a QueryOver like this

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

I've tried with Contains method but I've encountered the Exception

"System.Exception: Unrecognised method call: System.String:Boolean Contains(System.String)"

Here my code

var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)                                                                
  .JoinAlias(() => baseModel.Submodels, () => subModels)
  .Where(() => subModels.ID.Contains(IDsSubModels))
  .List<MyModel>();
Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
Faber
  • 2,194
  • 2
  • 27
  • 36

3 Answers3

69

I've found the solution!! :-)

var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)
    .JoinAlias(() => baseModel.Submodels, () => subModels)
    .WhereRestrictionOn(() => subModels.ID).IsIn(IDsSubModels)
    .List<MyModel>();
Nick DeVore
  • 9,748
  • 3
  • 39
  • 41
Faber
  • 2,194
  • 2
  • 27
  • 36
52

You can try something like this:

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

You don't need a join in this situation

LPL
  • 16,827
  • 6
  • 51
  • 95
Artem A
  • 2,154
  • 2
  • 23
  • 30
  • 3
    This would filter by MyModel.ID, not by MyModel.Submodels.ID as @Faber wanted, right? – kroimon Oct 09 '12 at 22:27
  • x here is the instance of MyModel class, same as instance\record of Table in your SQL request. SELECT * FROM Table WHERE Field IN (1,2,3,4,5) And x.ID in (1,2,3) is same as Table.Field in (1,2,3). 1,2,3 is IDsSubModels – Artem A Apr 27 '17 at 09:55
13

This works and is more elegant

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
Arnold
  • 131
  • 1
  • 3