假设我们使用Nhibernate进行以下查询:
var query = session.QueryOver<TenantOrder>(() => tenantOrderAlias)
.JoinAlias(() => tenantOrderAlias.Tenant, () => tenantAlias)
.JoinAlias(() => tenantAlias.Building, () => buildingAlias)
.WhereRestrictionOn(x => tenantOrderAlias.Id.OrderId).IsLike(order.Id);
query.Select(
Projections.Property(() => tenantAlias.Id).WithAlias(() => tenantDto.Id),
Projections.Property(() => tenantAlias.TenantNr).WithAlias(() => tenantDto.TenantNr),
Projections.Property(() => buildingAlias.BuildingNr).WithAlias(() => tenantDto.BuildingNr),
Projections.Property(() => tenantAlias.Floor).WithAlias(() => tenantDto.Floor),
Projections.Property(() => tenantOrderAlias.InstallationStatus).WithAlias(() => tenantDto.InstallationStatusName //? ));
我们希望将来自tenantOrderalias的InstallationStatus(类型枚举)处理为InstallationStatusName(来自tenantDto的string类型的属性)。枚举的结构如下所示:
public enum TenantInstallationStatusEnum
{
[StringEnum("MS3_TenantInstallationStatus_TS0")]
TS0,//open
[StringEnum("MS3_TenantInstallationStatus_TS2")]
TS2,//abgelesen
}
我们希望获得枚举的描述并将其放置到 tenantDto.InstallationStatusName 。这可以使用QueryOver
或ICriteria
吗?
我们找不到关于此问题的任何帖子。
答案 0 :(得分:1)
一般情况下,我们只能项目存在于数据库端的东西,或者作为&#34;公式&#34;传递。所以,我们可以将状态转换为这样的条件语句:
// this projection
Projections
.Property(() => tenantOrderAlias.InstallationStatus)
.WithAlias(() => tenantDto.InstallationStatusName //? ));
// could be converted into string values with this statement
Projections
.Conditional(
Restrictions.Where<TenantOrder>(to =>
to.InstallationStatus == TenantInstallationStatusEnum.TS0),
Projections.Constant("MS3_TenantInstallationStatus_TS0"),
Projections.Constant("MS3_TenantInstallationStatus_TS2")
).WithAlias(() => tenantOrderAlias.InstallationStatusName)
);
条件甚至可以嵌套......但最后它可能会更具挑战性。
但是,实际上可能更容易,就是在应用程序端以C#方式处理此转换事件......