nhibernate query, selecting only specific fields

83 views Asked by At

i have these entities: ClassB1 and ClassB2 are two separate entities. They have separate tables but the same fields.

Both of them have reference to ClassC which has a separate table.

ClassA
   -> ClassB1
         -> Property1
         -> ClassC
             -> Property2
   -> ClassB2
         -> Property1
         -> ClassC
             -> Property2

I need to query Property1 and Property2, so far I have this:

 ClassB1 classB1Alias = null;
 ClassB2 classB2Alias = null;
 ClassC classCAlias = null;


    var query = s.QueryOver<ClassA>();
    if (!string.IsNullOrWhiteSpace(searchFilter.Property1))
    {
        query.JoinAlias(d => d.ClassB1, () => classB1Alias );
        query.WhereRestrictionOn(() => classB1Alias.Property1)
            .IsLike("%" + searchFilter.Property1 + "%");

        query.JoinAlias(p => p.ClassB2, () => classB2Alias);
        query.WhereRestrictionOn(() => classB2Alias.Property1)
            .IsLike("%" + searchFilter.Property1+ "%");
    }
    if (!string.IsNullOrWhiteSpace(searchFilter.Property2))
    {
        query.JoinAlias(d => d.ClassB1.ClassC, () => classCAlias);
        query.JoinAlias(d => d.ClassB2.ClassC, () => classCAlias);
        query.WhereRestrictionOn(() => classCAlias.Property2)
           .IsLike("%" + searchFilter.Property2+ "%");
    }

    // select only Property1 and Property2 here

    return query.List();

I have seen projections but the samples are connected to a criteria object. Im just new to nhibernate and i have been coding all afternoon to get the correct query which should be

Select all ClassB1 and ClassB2 which have a Property1 equal to x where ClassB1.ClassC.Property2 and ClassB2.ClassC.Property2 equal to y

kinda need help here. Thanks

ps. is my query for Property2 correct?

1

There are 1 answers

0
Bruno Machado - vargero On

Wouldn't it suffice to use this?

query.Where(x => x.ClassB1Reference.Property1 == x && x.ClassB2Reference.Property1 == x).
Where(x => x.ClassB1Reference.ClassCReference.Property2 == y && x.ClassB2Reference.ClassCReference.Property2 == y);