C# 使用JoinAlias和相同别名创建nhibernate条件查询
我试图在NHibernate中创建一个condinotal查询,并且有一个别名在多个condinon中重新调整。 我正在获取并验证重复别名 如何解决这个问题 我的代码是:C# 使用JoinAlias和相同别名创建nhibernate条件查询,c#,nhibernate,C#,Nhibernate,我试图在NHibernate中创建一个condinotal查询,并且有一个别名在多个condinon中重新调整。 我正在获取并验证重复别名 如何解决这个问题 我的代码是: City parentCityAlias = null; Country parentCountryAlias = null; var streetsQuery = _session.QueryOver<Street>(); if (request.cityId.HasValue) { streetsQ
City parentCityAlias = null;
Country parentCountryAlias = null;
var streetsQuery = _session.QueryOver<Street>();
if (request.cityId.HasValue)
{
streetsQuery = streetsQuery.Where(t => t.ParentCity.Id == request.cityId);
}
if (request.countryId.HasValue)
{
streetsQuery = streetsQuery.JoinAlias(t => t.ParentCity, () => parentCityAlias)
.Where(() => parentCityAlias.ParentCountry.Id == request.countryId.Value);
}
IEnumerable<Street> streets = streetsQuery.List();
City parentCityAlias=null;
Country parentCountryAlias=null;
var streetsQuery=_session.QueryOver();
if(request.cityId.HasValue)
{
streetsQuery=streetsQuery.Where(t=>t.ParentCity.Id==request.cityId);
}
if(request.countryId.HasValue)
{
streetsQuery=streetsQuery.JoinAlias(t=>t.ParentCity,()=>parentCityAlias)
.Where(()=>parentCityAlias.ParentCountry.Id==request.countryId.Value);
}
IEnumerable streets=streetsQuery.List();
非常感谢您应该能够创建一个
if
语句来重构JOIN
,以便两种条件都可以使用它。即:
if (condition1 || condition2)
{
streetsQuery.JoinAlias(t => t.ParentCity, () => parentCityAlias);
}
if (condition1)
{
streetsQuery.Where(() => parentCityAlias.ParentCountry.Id == /* .. */);
}
if (condition2)
{
streetsQuery.Where(() => /* condition 2 */);
}
谢谢。但这只是一个小插曲。有很多条件和别名。我想要一个通用的解决方案。