Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JoinAlias和相同别名创建nhibernate条件查询_C#_Nhibernate - Fatal编程技术网

C# 使用JoinAlias和相同别名创建nhibernate条件查询

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

我试图在NHibernate中创建一个condinotal查询,并且有一个别名在多个condinon中重新调整。 我正在获取并验证重复别名

如何解决这个问题

我的代码是:

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 */);
}

谢谢。但这只是一个小插曲。有很多条件和别名。我想要一个通用的解决方案。