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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With