Maksym
Maksym

Reputation: 264

Exception in NHibernate query when use Contains

I have the following classes:

class Operation
{
    User User_AssignedTo;
    ResourceGroup ResourceGroup;
} 

class ResourceGroup
{
    List<User> UsersCollection;
}

And I have method. It takes user and returns operations. Something like that:

ResourceGroup resourceGroup = null;

query = conn.Session.QueryOver<Operation>()
        .JoinAlias(item => item.ResourceGroup, () => resourceGroup)
        .Where(item => item.User_AssignedTo.Id == user.Id || resourceGroup.UsersCollection.Contains(userDm));

but I have exception

Unrecognised method call: System.Collections.Generic.ICollection`1[[Mapping.Classes.User, Mapping.Classes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8ab89f53b66a52c3]]:Boolean Contains

Upvotes: 2

Views: 1019

Answers (2)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123901

The Contains is a C# function. In SQL we would use the MyProperty IN (Select ...). To achieve that with NHibernate, to have the IN clause and the inner select - we can use the DetachedCriteria:

The documentation: 15.8. Detached queries and subqueries

There are some detailed examples how to use it:

NOTE: I would like to provide you with more details even for your solution, some draft.. But the problem is the snippets in the question. To be able to use IN both parts should have some ID (the 1) ID to selected and 2) the ID to be compared). ResourceGroup is missing ID, hard to understand how the pairing tables behind are designed.

But at least the DetachedCriteria concept should give you correct direction...

Upvotes: 1

jle
jle

Reputation: 9489

NHibernate doesn't like the '||' operator. Try this: NHIbernate OR Criteria Query

Another example of how to do an Or operation: NHibernate JoinAlias query with null test not working

Upvotes: 0

Related Questions