Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific column with NHibernate Query

I have an application using NHibernate and C# language. I have an model with some relations and I want to create a query to get only one value. I've tried something like this:

public long GetIdCompany(long number)
{
    return session.QueryOver<Report>()
                  .Where(x => x.Number == number)
                  .Select(x => x.Equipament.Company.Id)
                  .Take(1);
}

but I didn't work. I just want to take the IdCompany in the the model Report.Equipament.Company.Id. It could be in queryover, linq, hql, etc...

like image 922
Felipe Oriani Avatar asked Dec 04 '22 02:12

Felipe Oriani


1 Answers

You should use JoinAlias, so your request will looks like

    public long GetIdCompany(long number)
    {
        Equipament equipamentAlias = null;
        return session.QueryOver<Report>()
                      .Where(x => x.Number == number)
                      .JoinAlias(x => x.Equipament, () => equipamentAlias)
                      .Select(x => equipamentAlias.Company.Id)
                      .SingleOrDefault<long>();
    }

Here is excellent introduction to QueryOver

like image 54
GSerjo Avatar answered Dec 20 '22 08:12

GSerjo