Nhibernate QueryOver Field1 = True OR(集合包含...)

发布于 2024-12-05 20:53:11 字数 1111 浏览 5 评论 0原文

我正在为搜索表单编写一个处理程序。正在搜索的对象有 3 个布尔标志和多个集合(如 ISet)。

这些标志表示集合中各个成员的抽象分组,因此它们是相关的。在本例中,集合代表县、城市和非常广泛的“地区”。布尔标志供作者指定实体 IsOutOfState、IsStatewide 和/或 IsEverywhere(例如 Internet)。

还有其他非地理集合(例如主题)。对此的任何限制都将与地理限制“和”在一起。

在搜索表单上,用户可以勾选感兴趣的特定地理区域(特定县和/或城市和/或地区),并扩大搜索范围以包括设置了一个或多个额外布尔标志的项目(如果它们不这样做)不要选中该框,我们不限制布尔字段)。

在 SQL 中手动编写此内容,我会执行以下操作:

SELECT ... FROM foo LEFT JOIN FooCounties fc ON foo.ID = fc.ID 
LEFT JOIN FooCities fct ON foo.ID = fct.ID ...
INNER JOIN FooTopics ft ON foo.ID = ft.ID
WHERE (fc.CountyID IN (1, 2, ...) OR fct.CityID IN (1, 3, 5, ...) OR foo.IsStatewide = 1)
      AND ft.TopicID IN (1, 4, 7, ...)

如何将其转换为 QueryOver?到目前为止,我已经通过如下方式获得了主题和其他 AND 标准:

var query = Session.QueryOver<foo>();

if (SelectedTopicIDs.Count > 0) { 
  var TopicSubQ = QueryOver.Of<foo>().JoinQueryOver<Topic>(t => t.Topics)... etc.
  query = query.WithSubquery.WhereProperty(p => p.Id).In(TopicSubQ);
}

但我无法弄清楚如何使用 Disjunction 来比较最多 3 个布尔值和最多 3 个子查询,并且然后将其作为“和”与现有标准合并。

I'm writing a handler for a search form. The object being searched has 3 boolean flags and several collections (as ISet)

These are related in that the flags represent abstract groupings of individual members of the collections. In this specific case, the collections represent Counties, Cities, and very broad "Regions." The boolean flags are for the author to specify that an entity IsOutOfState, IsStatewide, and/or IsEverywhere (e.g. Internet).

There are additional non-geographic collections (e.g. Topics). Any restriction on this would be "and"ed with the geographic restrictions.

On the search form, a user can check off particular geographic areas of interest (specific counties and/or cities and/or regions) as well as broadening their search to include items with one or more of the extra boolean flags set (if they don't check the box, we don't restrict the boolean fields).

Writing this manually in SQL, I'd do something like:

SELECT ... FROM foo LEFT JOIN FooCounties fc ON foo.ID = fc.ID 
LEFT JOIN FooCities fct ON foo.ID = fct.ID ...
INNER JOIN FooTopics ft ON foo.ID = ft.ID
WHERE (fc.CountyID IN (1, 2, ...) OR fct.CityID IN (1, 3, 5, ...) OR foo.IsStatewide = 1)
      AND ft.TopicID IN (1, 4, 7, ...)

How can I translate this to QueryOver? So far I've got the Topics and other AND criteria OK by something like this:

var query = Session.QueryOver<foo>();

if (SelectedTopicIDs.Count > 0) { 
  var TopicSubQ = QueryOver.Of<foo>().JoinQueryOver<Topic>(t => t.Topics)... etc.
  query = query.WithSubquery.WhereProperty(p => p.Id).In(TopicSubQ);
}

But I can't figure out how I would use Disjunction to compare up to 3 bools and up to 3 subqueries, and then merge that all as an 'and' with the existing criteria.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2024-12-12 20:53:11
var query = session.QueryOver<foo>();
var or = new Disjunction().Add(Restrictions.Where<foo>(f => f.IsStatewide));

if (SelectedCountryIDs.Count > 0)
{
    Country countryAlias = null;
    var join = query.JoinAlias(f => f.Countries, () => countryAlias);
    or.Add(() => countryAlias.Id.IsIn(SelectedTopicIDs));
}

query.Where(or);

if (SelectedTopicIDs.Count > 0)
{
    Topic topicAlias = null;
    var join = query.JoinQueryOver<Topic>(f => f.Topics)
                    .WithSubquery.Where(() => topicAlias.Id).In(SelectedTopicIDs));
}
var query = session.QueryOver<foo>();
var or = new Disjunction().Add(Restrictions.Where<foo>(f => f.IsStatewide));

if (SelectedCountryIDs.Count > 0)
{
    Country countryAlias = null;
    var join = query.JoinAlias(f => f.Countries, () => countryAlias);
    or.Add(() => countryAlias.Id.IsIn(SelectedTopicIDs));
}

query.Where(or);

if (SelectedTopicIDs.Count > 0)
{
    Topic topicAlias = null;
    var join = query.JoinQueryOver<Topic>(f => f.Topics)
                    .WithSubquery.Where(() => topicAlias.Id).In(SelectedTopicIDs));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文