NHibernate 3.1 Linq with Contains and Any

We are in the process of switching to NH3.1, which is going well - everything works, as far as we can judge by the existing code. One of the reasons for switching to NH3 out of 2 was to use Linq support and, as a rule, works very well. However, I am struggling with some of the more complex ones where clauses are, especially when I want to check based on subnets:

var results = from r in registrations where ( from p in persons where p.ExplicitManagers.Any(m => m.Manager == manager) select p ).Contains(r.Registrant) select r; 

where is the model:
p is Person , and a registration r has a registrar of type Person
p contains a set of associative objects ExplicitManager , which hold the link to another Person (manager).

Note: registration is IQueryable<Registration>.Query() and entities in IQueryable<Person>.Query() .
Essentially, I'm trying to limit the registration to where person1 is the explicit manager p . I can do this through connections, but not through the Contains subquery.

I get the following error:

"System.InvalidOperationException: The sequence contains more than one matching element"

the reason for this as a subquery is that in the end I need to externalize the logic for checking managers to make it reusable (it is actually more complicated, but I simplified it for this example because it is Any inside a Contains , which causes grief).

Contains seems to work fine when it doesn't have a subquery with Any. Am I doing something wrong, or is it something unsupported or a mistake, and is there any other way to achieve the same?

Thanks so much for any help you can give.

+7
source share
2 answers

While Contains not working properly, using Any does:

 var results = from r in registrations where ( from p in persons where p.ExplicitManagers.Any(m => m.Manager == manager) select p ).Any(p=>p == r.Registrant) select r; 
+7
source

Can you fulfill your own request without problems?

 var result = from p in persons where p.ExplicitManagers.Any(m => m.Manager == manager) select p; 
+1
source

All Articles