LINQ Fluent NHIBERNATE .Contains() does not work in QueryOver<> but works in Query<>

jaxxbo picture jaxxbo · Apr 7, 2013 · Viewed 12.6k times · Source

Using FNH, i am trying to retrieve categories, using the following:

_session.QueryOver<Data.Model.Category>()
                                     .Where(c => tourCreateRequest.Categories.Contains(c.CategoryId))
                                     .List()
                                     .Select(_categoryMapper.CreateCategory)
                                     .ToList();

But I get an error at the .Contains() method :

Unrecognised method call: System.Collections.Generic.ICollection`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]:Boolean Contains(Int64)

Why am I getting that error, what is wrong?

I went through some posts, and then changed my query to (below), and this works with Query<>.

_session.Query<Data.Model.Category>()
                                     .Where(c => tourCreateRequest.Categories.Contains(c.CategoryId))
                                     .ToList()
                                     .Select(_categoryMapper.CreateCategory)
                                     .ToList();

I thought QueryOver<> is the latest and greatest and should be used instead of Query<>.

What is the issue with the way I am using QueryOver<> as shown above?

Answer

jaxxbo picture jaxxbo · Apr 7, 2013

I found the answer. Thanks to the post at: NHibernate using QueryOver with WHERE IN

var categories = _session.QueryOver<Data.Model.Category>()
                                     .WhereRestrictionOn(c => c.CategoryId).IsIn(ArrayofCategoryIds)
                                     .List()
                                     .Select(_categoryMapper.CreateCategory)
                                     .ToList();

I had to use the WhereRestrictionOn()