无法解决属性错误
本文关键字:错误 属性 解决 | 更新日期: 2023-09-27 18:04:41
我正在尝试做这样的查询:
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不起作用。我哪里做错了?
据我所知,使用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