0

I am trying to do a query like:

  var a = session.QueryOver<Site>()
                  .SelectList(
                  x => x.Select(p => p.SiteName)
                 .Select(p => p.SiteId).Select(p => p.RegionLocation.City))
                 .List<object[]>();

but I get the error

could not resolve property: RegionLocation.City of: Entities.Site

The property exists and I can retrieve it using LINQ but QueryOver does not work. What am I doing wrong ?

Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169

1 Answers1

0

As far as i remember, with QueryOver, you have to join all entities in the association in order to be able to access it's properties.

This means you ought to do something like: (notice the .JoinQueryOver)

var a = session.QueryOver<Site>()
             .JoinQueryOver(s => s.RegionLocation)
             .SelectList(
                x => x.Select(p => p.SiteName)
                      .Select(p => p.SiteId)
                      .Select(p => p.RegionLocation.City))
             .List<object[]>();

Or maybe this will work:

RegionLocation regionLocationAlias = null;
var a = session.QueryOver<Site>()
             .JoinAlias(s => s.RegionLocation, () => regionLocationAlias)
             .SelectList(
                x => x.Select(p => p.SiteName)
                      .Select(p => p.SiteId)
                      .Select(() => regionLocationAlias.City))
             .List<object[]>();

Also you might want to have a look at https://github.com/davybrion/NHibernateWorkshop/tree/master/NHibernateWorkshop There's lots of great examples!

Specifically for your problem, have a look at: https://github.com/davybrion/NHibernateWorkshop/blob/master/NHibernateWorkshop/Querying/QueryOver/Projecting.cs

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68