The solution is simple. *Use primary key (Id)* to compare to null: s.ClassC.
*Id* == null

Then you'll get:

    select
        classa0_.Id as col_0_0_,
        (select
            cast(count(*) as INT)
        from
            [ClassB] classb1_
        where
            classb1_.Id is null) as col_1_0_
    from
        [ClassA] classa0_

The reason is that NHibernate is smart enough to recognize primary key of 
an entity and skips fetching related table. I comes handy when you would 
create more complex queries.

It definitely solves the problem for the simplified example, you've shared, 
but what about the real-life use case? Let me know if it helps :)

On Thursday, August 20, 2015 at 10:50:55 AM UTC+2, Иван Иванов wrote:
>
> Hello.
>
>  public class ClassA : IEntity
>     {
>         public virtual int Id { get; set; }
>
>         public virtual string Name { get; set; }
>     }
>
>     public class ClassB : IEntity
>     {
>         public virtual int Id { get; set; }
>
>         public virtual string Name { get; set; }
>
>         public virtual ClassC ClassC { get; set; }
>     }
>
>     public class ClassC : IEntity
>     {
>         public virtual string Name { get; set; }
>
>         public virtual int Id { get; set; }
>     }
>
> Query:
>   var list = session
>                     .Query<ClassA>()
>                     .Select(x => new
>                         {
>                             x.Id,
>                             Count = session.Query<ClassB>().Count(s => 
> s.ClassC == null)
>                         })
>                     .ToList();
>
> Result:
>
> select classa0_.CLASS_A_ID                        as col_0_0_,
>        (select cast(count(*) as INT)
>         from   CLASS_B classb1_,
>                CLASS_C classc2_
>         where  classb1_.CLASS_C_ID = classc2_.CLASS_C_ID
>                and (classb1_.CLASS_C_ID is null)) as col_1_0_
> from   CLASS_A classa0_
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nhusers+unsubscr...@googlegroups.com.
To post to this group, send email to nhusers@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to