Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#NHibernate根聚合筛选子集合和子查询_C#_Nhibernate - Fatal编程技术网

C#NHibernate根聚合筛选子集合和子查询

C#NHibernate根聚合筛选子集合和子查询,c#,nhibernate,C#,Nhibernate,我试图检索根元素及其子集合的列表,在子集合中应用子查询过滤器。此子查询需要带来根的子元素的最后一次出现 预期结果是根元素的列表,其中每个根元素只有其子元素在子集合属性上的最后一次出现 这些元素的类表示: public class Root { public virtual long id { get; set; } public virtual ISet<Child> childList { get; set; } } public class Child { p

我试图检索根元素及其子集合的列表,在子集合中应用子查询过滤器。此子查询需要带来根的子元素的最后一次出现

预期结果是根元素的列表,其中每个根元素只有其子元素在子集合属性上的最后一次出现

这些元素的类表示:

public class Root {
   public virtual long id { get; set; }
   public virtual ISet<Child> childList { get; set; }
}

public class Child {
   public virtual long id { get; set; }
   public virtual DateTime occurrence { get; set; }
   public virtual Root parent { get; set; }
}
公共类根目录{
公共虚拟长id{get;set;}
公共虚拟ISet子列表{get;set;}
}
公营儿童{
公共虚拟长id{get;set;}
公共虚拟日期时间事件{get;set;}
公共虚拟根父{get;set;}
}
我曾尝试使用NHibernate的QueryOver创建一个查询,不幸的是,它没有按预期工作

这是我尝试过的代码:

Root rootAlias = null;
Child childAlias = null;

var query = QueryOver.Of(() => rootAlias)
   .Left.JoinAlias(
      () => rootAlias.childList, 
      () => childAlias, 
      Restrictions.Where<Child>(
         cd => cd.occurrence == QueryOver.Of<Child>()
            .Where(cx => cx.parent.id == rootAlias.id)
            .Select(cx => cx.occurrence)
            .OrderBy(cx => cx.occurrence).Desc
            .Take(1)
            .As<DateTime>()
   )                    
);
Root rootAlias=null;
Child-childAlias=null;
var query=QueryOver.Of(()=>rootAlias)
.Left.JoinAlias(
()=>rootAlias.childList,
()=>儿童别名,
限制,在哪里(
cd=>cd.occurrence==QueryOver.Of()
.Where(cx=>cx.parent.id==rootAlias.id)
.Select(cx=>cx.occurrence)
.OrderBy(cx=>cx.occurrence).Desc
.采取(1)
.As()
)                    
);

如果你们能帮我解决这个问题,或者甚至为我指出另一种达到上述结果的方法,我会很高兴。

在搜索和阅读了数百篇类似的文章后,我找到了解决我问题的方法

正如我在问题中所说:“预期的结果是根元素的列表,其中每个元素只有其子元素在子集合属性上的最后一次出现。”

考虑到我的课程结构,这就是解决方案:

// Aliases
Root rootAlias = null;
Child x_temp = null;
Child childAlias = null;

// Result list.
IList<Root> roots = null;

// Subquery which retrieves a list of child's id by max occurrence and grouping by 
// root id.
var subquery = CurrentSession.QueryOver<Child>(() => x_temp)
    .SelectList(list => list
        .Select(() => x_temp.id)
        .SelectMax(() => x_temp.occurrence)
        .SelectGroup(() => x_temp.root.id)
    )
    .List<object[]>()
        .Select(p => p[0]).ToArray();

// Query root elements, joining with child collection, applying
// a restriction on child's id attribute through 
// JoinAlias' "With clause" parameter.
var query = QueryOver.Of(() => rootAlias).Left.JoinAlias(
    () => rootAlias.childList,
    () => childAlias,
    Restrictions.On(() => childAlias.id).IsIn(subquery)
);

// retrieves the final result list through the query.
roots = query.GetExecutableQueryOver(CurrentSession).List();
//别名
Root rootAlias=null;
子x_temp=null;
Child-childAlias=null;
//结果清单。
IList根=空;
//子查询,按最大出现次数和分组方式检索子id列表
//根id。
var subquery=CurrentSession.QueryOver(()=>x_temp)
.SelectList(list=>list
.选择(()=>x_温度id)
.SelectMax(()=>x_温度出现)
.SelectGroup(()=>x_temp.root.id)
)
.List()
.Select(p=>p[0]).ToArray();
//查询根元素,与子集合联接,应用
//通过对子对象id属性的限制
//JoinAlias的“With子句”参数。
var query=QueryOver.Of(()=>rootAlias).Left.JoinAlias(
()=>rootAlias.childList,
()=>儿童别名,
限制(()=>childAlias.id).IsIn(子查询)
);
//通过查询检索最终结果列表。
root=query.GetExecutableQueryOver(CurrentSession.List();
就这样

我希望它能对其他有类似问题的人有用