0

I have a scenario that I have to check the table id list with the given string list and get all the relevant data.I know how to retrieve the data for one equal criteria.

var criteria = sm.Session.CreateCriteria(GetRegisteredType<Data>());

criteria = criteria.Add(Expression.Eq("Id", "9999"));

I have string list that I need to check with Id column. Can anyone help with me this?

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
Kate Fernando
  • 381
  • 1
  • 4
  • 18
  • Possible duplicate of [nhibernate query using criteria for list of value type](https://stackoverflow.com/questions/28094096/nhibernate-query-using-criteria-for-list-of-value-type) – Frédéric May 26 '17 at 14:59
  • You have your answer in above linked question. If you need to do that with a subquery, see [this one](https://stackoverflow.com/q/30242846/1178314). – Frédéric May 26 '17 at 15:01

2 Answers2

1

Do you need to use an ICriteria-based query? If not, a LINQ-based query would look something like:

var results = 
    sm.Session
        .Query<Data>()
        .Where(d => listOfStrings.Contains(d.Id))
        .ToList();

However, you'll need to be wary of the size of your string list, as they will be passed as parameters and will be subject to a limit.

David Osborne
  • 6,436
  • 1
  • 21
  • 35
1

Let's assume you have the list:

var list = new string[] { "9999", "1111" };
  1. Criteria

    Session.CreateCriteria(GetRegisteredType<Data>()).Add(Restrictions.InG("Id", list));
    
  2. QueryOver

    Session.QueryOver<Data>().Where(x => x.Id.IsIn(list));
    
  3. NHibernate.LINQ

    Session.Query<Data>().Where(x => list.Contains(x.Id))
    
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59