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

时间:2015-12-23 10:19:03

标签: c# nhibernate

我正在尝试在NHibernate中创建一个condiniotal查询,并且有一个别名在多个condidion中重新调整。 我正在获取重复别名

解决此问题的方法是什么?

我的代码是:

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();

非常感谢

1 个答案:

答案 0 :(得分:0)

您应该能够创建一个if语句来重构JOIN,以便两个条件都可以使用它。那就是:

if (condition1 || condition2)
{
    streetsQuery.JoinAlias(t => t.ParentCity, () => parentCityAlias);
}

if (condition1)
{
    streetsQuery.Where(() => parentCityAlias.ParentCountry.Id == /* .. */);
}

if (condition2)
{
    streetsQuery.Where(() => /* condition 2 */);
}
相关问题