具有内连接和子查询的 NHibernate Criteria 引擎

发布于 2024-12-12 07:32:31 字数 1305 浏览 0 评论 0原文

是否可以在 NHibernate 中创建如下所示的查询?

select hi.ContactId
From dbo.vw_HostInterests hi INNER JOIN
    (  Select cm1.ContactId
        From dbo.vw_ContactMoments cm1 INNER JOIN
            (
                Select Contactid
                From dbo.vw_ProfileNaw
                where GenderId = 1000
            ) as pn1 on cm1.ContactId = pn1.ContactId
        where cm1.ActivityId = 1001
    )as cm on hi.ContactId = cm.ContactId

where hi.ActivityId = 1038

我已经设法使用 IN 语句创建正确的输出,但我真的希望 SQL 看起来像这样。 下面的条件显示了上述查询的一部分以及我使用的 IN 语句(但想要替换):

ICriteria criteria = DbSession.CreateCriteria<Contact>();

var dCriteria1 = DetachedCriteria.For(typeof(VwHostInterest))
    .Add(Expression.Eq("ActivityId", 1038))
    .SetProjection(Projections.ProjectionList()
    .Add(Projections.GroupProperty("ContactId")));

var dCriteria2 = DetachedCriteria.For(typeof(VwContactMoment))
    .Add(Expression.Eq("ActivityId", 1001))
    .SetProjection(Projections.ProjectionList()
    .Add(Projections.GroupProperty("ContactId")));

criteria.Add(Subqueries.PropertyIn("ContactId", dCriteria1));
criteria.Add(Subqueries.PropertyIn("ContactId", dCriteria2));

int count = (Int32)criteria
    .SetProjection(Projections.Count("ContactId"))
    .UniqueResult();

Is it posible in NHibernate to create a query that looks like this?

select hi.ContactId
From dbo.vw_HostInterests hi INNER JOIN
    (  Select cm1.ContactId
        From dbo.vw_ContactMoments cm1 INNER JOIN
            (
                Select Contactid
                From dbo.vw_ProfileNaw
                where GenderId = 1000
            ) as pn1 on cm1.ContactId = pn1.ContactId
        where cm1.ActivityId = 1001
    )as cm on hi.ContactId = cm.ContactId

where hi.ActivityId = 1038

I've managed to create the correct output with the IN statement, but I'd realy like the SQL to look like this.
The Criteria below shows part of above query with the IN Statement I used (but want to replace):

ICriteria criteria = DbSession.CreateCriteria<Contact>();

var dCriteria1 = DetachedCriteria.For(typeof(VwHostInterest))
    .Add(Expression.Eq("ActivityId", 1038))
    .SetProjection(Projections.ProjectionList()
    .Add(Projections.GroupProperty("ContactId")));

var dCriteria2 = DetachedCriteria.For(typeof(VwContactMoment))
    .Add(Expression.Eq("ActivityId", 1001))
    .SetProjection(Projections.ProjectionList()
    .Add(Projections.GroupProperty("ContactId")));

criteria.Add(Subqueries.PropertyIn("ContactId", dCriteria1));
criteria.Add(Subqueries.PropertyIn("ContactId", dCriteria2));

int count = (Int32)criteria
    .SetProjection(Projections.Count("ContactId"))
    .UniqueResult();

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

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

发布评论

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

评论(2

你的笑 2024-12-19 07:32:31

可能不是您正在寻找的答案,如果不是,请道歉,但我的经验是,处理如此复杂的查询的最佳选择是:

a)将整个事情作为视图进行,并将其映射到 NHibernate

b)创建内部选择作为视图并创建映射,以便您可以在查询中将其关联

b) 或覆盖 nhibernate(以跳过方式覆盖,而不是 OO 术语;)并使用本机 SQL 将其编写为命名查询。

Probably not the answer you are looking for and apologies if it isn't but my experience is that your best bet with such complex queries would be to:

a) Do this whole thing as a view and map it in NHibernate

b) Create the inner select as a view and create a mapping such that you can relate it in your query

b) Or override nhibernate (override as in skip and not in OO terms ;) and write this as a named query using native SQL.

飘过的浮云 2024-12-19 07:32:31

该嵌套查询是否产生与以下相同的结果?

SELECT hi.ContactId
FROM dbo.vw_HostInterests hi 
INNER JOIN vw_ContactMoments cm1 on hi.ContactId = cm1.ContactId
    AND cm1.ActivityId = 1001
INNER JOIN dbo.vw_ProfileNaw pn1 on pn1.ContactId = cm1.ContactId
    AND pn1.GenderId = 1000   
WHERE hi.ActivityId = 1038   

Does that nested query produce the same result as the following?

SELECT hi.ContactId
FROM dbo.vw_HostInterests hi 
INNER JOIN vw_ContactMoments cm1 on hi.ContactId = cm1.ContactId
    AND cm1.ActivityId = 1001
INNER JOIN dbo.vw_ProfileNaw pn1 on pn1.ContactId = cm1.ContactId
    AND pn1.GenderId = 1000   
WHERE hi.ActivityId = 1038   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文