c# - 流畅的 nHibernate 查询 => QueryOver、Join、Distinct

标签 c# fluent-nhibernate

我尝试将该查询更改为 QueryOver<>能够做到 Distinct在(生成的 sql)查询中进行操作

var result = (from x in Session.Query<Events>()
              join o in Session.Query<Receivers>() on x.ID equals o.ID
              where x.Owner.ID == 1 //the user is the owner of that Event (not null)
                    ||
                    x.EVType.ID == 123 //(not null)
                    ||
                    x.Receivers.Count(y => y.User.ID == 1) > 0 //the user is one of the Event Receivers
              select x.StartDate)
              .Distinct();

我试过类似的东西

Events x = null;
List<Receivers> t = null;

var result = Session.QueryOver<Events>(() => x)
                    .JoinAlias(() => x.Receivers, () => t)
                    .Where(() => x.Owner.ID == 1
                                ||
                                x.EVType.ID == 123
                                ||
                                t.Count(y => y.User.ID == 1) > 0)
                   .TransformUsing(Transformers.DistinctRootEntity)
                   .Select(a => a.StartDate)
                   .List();

但后来我得到了 Value can not be null. Parameter name: source异常(exception)。有什么想法可以解决该查询吗?

编辑

感谢 xanatos 的回答,最终的 SQL 查询是正确的(我使用了他的第二种方法):

SELECT distinct this_.StartDate as y0_ 
FROM Events this_ 
WHERE 
(
    this_.UserID = ? 
    or
    this_.EventTypeID = ? 
    or
    exists (SELECT this_0_.ID as y0_ 
            FROM Receivers this_0_ 
            WHERE this_0_.UserID = ?)
)

最佳答案

“在 QueryOver 中,别名是使用空变量分配的。变量可以在任何地方声明(但在运行时应该为空/默认值)。然后编译器可以检查语法是否正确使用了变量,但是在运行时,不会评估变量(它只是用作别名的占位符)。"http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html

设置List<Receivers> t像您所做的那样清空集合(正如您在评论中提到的那样)意味着您检查的是本地空集合中的事件 ID - 根本没有意义。

您可以尝试使用子查询进行查询(应该可以,但我不确定,我是在没有测试的情况下“手动”编写的):

Receivers receiversSubQueryAlias = null;
var subquery = session.QueryOver<Events>()
                      .JoinQueryOver<Receivers>(x => x.Receivers, () => receiversSubqueryAlias, JoinType.Inner)
                      .Where(()=> receiversSubQueryAlias.UserId == 1)
                      .Select(x => x.Id)
                      .TransformUsing(Transformers.DistinctRootEntity);

Events eventsAlias = null;
var mainQueryResults = session.QueryOver<Events>(() => eventsAilas)
                       .Where(Restrictions.Disjunction()
                             .Add(() => eventAlias.OwnerId == 1)
                             .Add(() => eventAlias.EVType.Id == 123)
                             .Add(Subqueries.WhereProperty<Events>(() => eventAlias.Id).In(subquery))
                        ).Select(x => x.StartDate)
                        .TransformUsing(Transformers.DistinctRootEntity)
                        .List();

关于c# - 流畅的 nHibernate 查询 => QueryOver、Join、Distinct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28610692/

相关文章:

c# - Fluent NH 和接口(interface)映射

fluent-nhibernate - NHibernate 多对多映射无效列任意 [table]_[id]

queryover 中的 NHibernate View 返回重复记录

c# - 为什么我不能直接访问已分配给 WinForms Control.Tag 属性的自定义对象的属性?

c# - 简单的 C# ASP.NET 缓存实现

c# - 32 位应用程序未更新 64 位注册表项

c# - 好的/简单的 asp.net mvc 应用程序与流利的 nhibernate

c# - Fluent NHibernate 或 NHibernate for Linq?

c# - Gridview 模板行中的条件评估

c# - 用sqlite和winrt编译报错