gpt4 book ai didi

linq - 无法解析属性(复杂属性)

转载 作者:行者123 更新时间:2023-12-04 05:39:56 25 4
gpt4 key购买 nike

我有一个带有 NHibernate 的 asp.net mvc 应用程序,但我不知道如何解决查询某些数据的问题。我有这个查询:

// create query
var query = session.QueryOVer<Laudo>().Fetch(x => x.Equipament).Eager;

// add some filters
if (idEquipament.HasValue)
query = query.And(x => x.Equipament.Id == idEquipament.Value);

//I got the error here...
if (idCompany.HasValue)
query = query.And(x => x.Equipament.Company.Id == idCompany.Value);

当我尝试执行此查询时,此消息出现异常:
“无法解析属性:Equipament.Company.Id of:DomainModel.Laudo”

我该怎么做才能解决这个问题?
谢谢

最佳答案

您不能像这样使用另一个实体属性。 NHibernate 期望表达式可以评估为当前实体的属性。您需要使用 JoinQueryOver JoinAlias 加入另一个实体,并执行 where在那之后。

JoinQueryOver :

// ...
query = query.JoinQueryOver(x => x.Equipment)
.JoinQueryOver(x => x.Company)
.Where(c => c.Id == idCompany.Value);

JoinAlias :
Equipment equipment = null;
Company company = null;

// ...
query = query.JoinAlias(x => x.Equipment, () => equipment)
.JoinAlias(() => equipment.Company, () => company)
.Where(() => company.Id == idCompany.Value);

更多信息:
What is the difference between JoinQueryOver and JoinAlias?
What can be used as a NHibernate QueryOver alias?
Complex nHibernate QueryOver expression

关于linq - 无法解析属性(复杂属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11386533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com