使用 NHibernate Criteria API 创建嵌套 OR 语句

发布于 2025-01-05 18:32:48 字数 1808 浏览 0 评论 0原文

所以我喜欢学习 NHibernate 的新东西,因为它确实非常灵活,让我的生活变得轻松,除了我现在陷入的困境;)。到目前为止,我还没有做过非常复杂的查询,这个查询也不复杂,但我觉得最常用的方法和对象类型不会涵盖这个问题。

这是我想要查询的内容:

SELECT _Table1.UserID, _Table1.ColorID, _Table1.AppID FROM _Table1
INNER JOIN _Table2 ON _Table2.AppID = _Table1.AppID 
WHERE _Table1.ColorID= @param0
AND (
      _Table2.WindowID = @param1 OR
      _Table2.WindowID = @param2 OR
      _Table2.WindowID = @param3 OR
      ................ = @param(N - 1)
    ) 

这是我使用条件 API 得到的内容

private IList<Table1Entity> FetchListByWindowId(int colorID, List<int> listOfWindowIDs)
{

    var list = CurrentSession.CreateCriteria<Table1Entity>()
                 .Add(Restrictions.Eq("ColorID", colorID))
                 .CreateCriteria("Table2EntityList");

    foreach (var item in listOfWindowIDs)
    {
        list.GetCriteriaByPath("Table2EntityList")
            .Add(Restrictions.Eq("WindowID", Int32.Parse(item)
    }
}

表 1 的映射包括 4 个属性 UserID、ColorID、AppID 和一个到 Table2 实体的 HasMany。

HasMany<Table2Entity>(x => x.Table2EntityList)
     .KeyColumns.Add("AppID")
     .Not.LazyLoad()
     .Inverse()
     .Cascade.None();

从我投入寻找解决方案的一整天来看,我认为没有一种简单的方法可以明确地说“使这些表达式为 OR”(是的,我已经查看了 Expression.OR,但这不是我想要的)。

NHibernate 给我的查询正是我想要的,只是我需要用“OR”替换一些“AND”。

谢谢,如果这变得非常微不足道,抱歉。我还想为我的代码的模糊性道歉,但我处理敏感数据。所以所有的变量名和方法名等都必须调整。

编辑: NHibernate 当前给我的查询:

SELECT _Table1.UserID, _Table1.ColorID, _Table1.AppID FROM _Table1
INNER JOIN _Table2 ON _Table2.AppID = _Table1.AppID 
WHERE _Table1.ColorID= @param0
AND (
      _Table2.WindowID = @param1
      AND _Table2.WindowID = @param2 
      AND _Table2.WindowID = @param3
      AND ................ = @param(N - 1)
    ) 

So I love learning new things about NHibernate because it truly is very flexible and makes my life easy except for where i'm stuck at right now ;) . So far I haven't had to do very complex queries, and this one isn't complex either, but I feel that the most commonly used methods and object types won't cover this problem.

Here is what I want to query:

SELECT _Table1.UserID, _Table1.ColorID, _Table1.AppID FROM _Table1
INNER JOIN _Table2 ON _Table2.AppID = _Table1.AppID 
WHERE _Table1.ColorID= @param0
AND (
      _Table2.WindowID = @param1 OR
      _Table2.WindowID = @param2 OR
      _Table2.WindowID = @param3 OR
      ................ = @param(N - 1)
    ) 

and here is what I have using the criteria API

private IList<Table1Entity> FetchListByWindowId(int colorID, List<int> listOfWindowIDs)
{

    var list = CurrentSession.CreateCriteria<Table1Entity>()
                 .Add(Restrictions.Eq("ColorID", colorID))
                 .CreateCriteria("Table2EntityList");

    foreach (var item in listOfWindowIDs)
    {
        list.GetCriteriaByPath("Table2EntityList")
            .Add(Restrictions.Eq("WindowID", Int32.Parse(item)
    }
}

The mapping for table one includes 4 properties UserID, ColorID, AppID, and a HasMany to the Table2 entity.

HasMany<Table2Entity>(x => x.Table2EntityList)
     .KeyColumns.Add("AppID")
     .Not.LazyLoad()
     .Inverse()
     .Cascade.None();

From the full day I've put into searching for a solution, I don't think there is an easy way to explicitly say "Make these expressions OR" (yes I have looked at Expression.OR and that's not what I want).

The query NHibernate is giving me is exactly what I want except that I need to replace some "ANDs" with "ORs"

Thanks, and sorry if this turns out to be very trivial. I also want to apologize for the vagueness of my code, but I deal with sensitive data. So all variable names and method names and the like had to be adjusted.

EDIT:
The query that NHibernate is currently giving me:

SELECT _Table1.UserID, _Table1.ColorID, _Table1.AppID FROM _Table1
INNER JOIN _Table2 ON _Table2.AppID = _Table1.AppID 
WHERE _Table1.ColorID= @param0
AND (
      _Table2.WindowID = @param1
      AND _Table2.WindowID = @param2 
      AND _Table2.WindowID = @param3
      AND ................ = @param(N - 1)
    ) 

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

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

发布评论

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

评论(1

凉薄对峙 2025-01-12 18:32:48

像这样的东西:

var disjunction = new Disjunction()
    .Add(Restriction.Eq("WindowID", item1))
    .Add(Restriction.Eq("WindowID", item2))
    .Add(Restriction.Eq("WindowID", item3));
// Or use a loop if you like...

var list = CurrentSession.CreateCriteria<Table1Entity>()
    .Add(Restrictions.Eq("ColorID", colorID))
    .CreateCriteria("Table2EntityList")
    .Add(disjunction);

Something like:

var disjunction = new Disjunction()
    .Add(Restriction.Eq("WindowID", item1))
    .Add(Restriction.Eq("WindowID", item2))
    .Add(Restriction.Eq("WindowID", item3));
// Or use a loop if you like...

var list = CurrentSession.CreateCriteria<Table1Entity>()
    .Add(Restrictions.Eq("ColorID", colorID))
    .CreateCriteria("Table2EntityList")
    .Add(disjunction);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文