NHibernate:无法解决属性错误

时间:2014-05-23 10:34:38

标签: c# nhibernate

我正在尝试进行如下查询:

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

但我收到了错误

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

该属性存在,我可以使用LINQ检索它,但QueryOver不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:0)

据我所知,使用QueryOver,您必须加入关联中的所有实体才能访问它的属性。

这意味着你应该做的事情如下: (注意.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[]>();

或许这可行:

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[]>();

您也可以查看https://github.com/davybrion/NHibernateWorkshop/tree/master/NHibernateWorkshop 有很多很好的例子!

专门针对您的问题,请查看:https://github.com/davybrion/NHibernateWorkshop/blob/master/NHibernateWorkshop/Querying/QueryOver/Projecting.cs