Search code examples
nhibernatefluent-nhibernatequeryover

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


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.


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