Restrict fluent nhibernate queryover on multiple child entities comparing properties with a single value

StackOverflow https://stackoverflow.com/questions/22382534

Question

I would like to restrict a query using the QueryOver mechanism in Fluent Nhibernat. I found that I can do this using WhereRestrictOn but there seems to be no possibility to compare to just one value. A IsEqual method of sorts.

I quick example might explain better what my issue is

class Parent{
   IList<Child1> C1 {get;set;}
   IList<Child2> C2 {get;set;}
}

class Child1{
   int Id {get;set;}
}

class Child2{
   int Id {get;set;}
}

//What I can do
var result = session.QueryOver<Parent>()
   .WhereRestrictionOn(x => x.C1.Id).IsIn(new[]{3})
   .AndRestrictionOn(x => x.C2.Id).IsIn(new[]{5}).List();

//What I would like to do
var result = session.QueryOver<Parent>()
   .WhereRestrictionOn(x => x.C1.Id).IsEqual(3)
   .AndRestrictionOn(x => x.C2.Id).IsEqual(5).List();

So basically my issue is that I'm not able to compare with one value but always have to artifically create an array. Is this not possible or am I missing something?

If it is possible please tell me how. If it is not possible I would appreciate an explantion as to why not.

Thanks in advance.

Was it helpful?

Solution

Try this:

Child1 c1Alias = null;
Child2 c2Alias = null;

var result = session.QueryOver<Parent>()
   .InnerJoin(x => x.C1, () => c1Alias) // or use Left.JoinAlias
   .InnerJoin(x => x.C2, () => c2Alias) // or use Left.JoinAlias
   .Where(() => c1Alias.Id == 3)
   .And(() => c2Alias.Id == 2)
   .List();

OTHER TIPS

I think you want a pair of Subqueries rather than a restriction, but you'll have to map the ParentID value in both Child1 and Child2.

Each SubQuery should return the ParentID where the childID is your search value, and then you can use a conjunction to return the Parent for both children, or null if there isn't one, I suppose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top