Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 224
jaxxbo Avatar asked Apr 07 '13 13:04

jaxxbo


People also ask

How do I write a query in NHibernate?

Simple LINQ Queries are treated as NHibernate queries, but you have to connect them to repositories. Connect entities with Repository and IRepository to reduce its complexities. And it will be more structured. Let me know if I have taken you to right track or not.

What is QueryOver?

QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver.


2 Answers

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()

like image 152
jaxxbo Avatar answered Sep 19 '22 16:09

jaxxbo


This is tangentially related issue and this seemed like the best place to put it.

_session.Query<SomeType>.Where(t => someEnumerable.Contains(t)) 

was not working.

In my case someEnumerable was NOT a List<SomeType>, but rather a HashSet<SomeType>. Apparently, NH really wants it to be a list. So, I did this instead and it worked.

var someEnumerableList = someEnumerable.ToList(); _session.Query<SomeType>.Where(t => someEnumerableList.Contains(t) 

Also, FWIW, I was under the impression that Query<T> was the new preferred way to go and that QueryOver<T> was the less preferred way, because Query<T> returns IQueryable, meaning that it should be a little easier to test, and theoretically swap out ORMs.

like image 29
viggity Avatar answered Sep 22 '22 16:09

viggity