I have these entities in my database

For a given Job, I want to basically get the results in the following form
<Translation>,<ExternalUnit.Text>,<ExternalTranslation.Text>
where the joining condition is that Translation.Unit.Text == ExternalUnit.Text
This is what I have so far, working fine:
var props = session.QueryOver<Translation>(() => translation)
.Select(c => translation.Id, c => externalUnit.Text, c => externalTranslation.Text)
.JoinAlias(() => translation.TranslationUnit, () => unit)
.JoinAlias(() => unit.Job, () => job)
.Where(() => unit.Job == job)
.JoinAlias(() => job.ExternalUnits, () => externalUnit)
.JoinAlias(() => externalUnit.ExternalTranslations, () => externalTranslation)
.Where(() => externalUnit.Text == unit.Text)
.List<object[]>();
var translations = session.QueryOver<Translation>(() => translation)
.JoinAlias(() => translation.TranslationUnit, () => unit)
.JoinAlias(() => unit.Job, () => job)
.Where(() => unit.Job == job)
.JoinAlias(() => job.ExternalUnits, () => externalUnit)
.JoinAlias(() => externalUnit.ExternalTranslations, () => externalTranslation)
.Where(() => externalUnit.Text == unit.Text)
.List<Translation>()
.ToList();
Then I loop through translations, referring to props. However, I don’t like this approach since I unnecessarily perform two (almost identical) queries to the database instead of just one.
But I can’t get the desired projection working. I was thinking about something like this:
var data = session.QueryOver<Translation>(() => translationAlias)
.JoinAlias(() => translation.TranslationUnit, () => unit)
.JoinAlias(() => unit.Job, () => job)
.Where(() => unit.Job == job)
.JoinAlias(() => job.ExternalUnits, () => externalUnit)
.JoinAlias(() => externalUnit.ExternalTranslations, () => externalTranslation)
.Where(() => externalUnit.Text == unit.Text)
.Select(() => translation, () => externalUnit.Text, () => externalTranslation.Text)
.List()
but, obviously, NHibernate does not like the Select(() => translation...) bit (it does not allow me to project the whole entity).
Ideally I would like to select into anonymous types, like
var data = session.QueryOver<Translation>()
...
.Select(() => new { A = translation, B = externalTranslation })
but I guess NHibernate is not there so far…
Thank you very much for any suggestion.
I’ve got it! NHibernate’s LINQ provider saved me. What’s great about it is that it can select into anonymous types, which makes joining so much easier. Just in case someone is curious, here it is for my particular case:
The SQL it generates is very reasonable, so I am pretty happy 🙂