Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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# 如何返回至少包含所有预定义子级的所有父级_C#_Hibernate_Nhibernate_Fluent Nhibernate_Queryover - Fatal编程技术网

C# 如何返回至少包含所有预定义子级的所有父级

C# 如何返回至少包含所有预定义子级的所有父级,c#,hibernate,nhibernate,fluent-nhibernate,queryover,C#,Hibernate,Nhibernate,Fluent Nhibernate,Queryover,示例类(多对多关系): 公共类父类 { Guid Id; 孤独症儿童; } 公营儿童 { Guid Id; 亲缘关系; } 让我们考虑所有映射都被实现和正确工作。 我们在存储库中有一个方法: public class MyRepository { public IList<Parent> GetParents(IList<Child> children) { return Session.QueryOver ... ?

示例类(多对多关系):

公共类父类
{
Guid Id;
孤独症儿童;
}
公营儿童
{
Guid Id;
亲缘关系;
}

让我们考虑所有映射都被实现和正确工作。

我们在存储库中有一个方法:

public class MyRepository
{
     public IList<Parent> GetParents(IList<Child> children)
     {
          return Session.QueryOver ... ?
     }
}
公共类MyRepository
{
公共IList GetParents(IList子项)
{
return Session.QueryOver?
}
}
使用QueryOver API,我想返回所有有所有孩子的父母。父母可以有多个已定义的子女,但已定义的子女必须存在


问题会是怎样的

NHibernate
QueryOver
Criteria
做事的方式不是那么直观(至少对我来说是这样)。在LINQ中,您可以使用以下查询:

return Db.Parents.Where(x => 
    x.Children.Count(c => children.Contains(c)) == children.Length);
或(靠近下面的NHibernate溶液)

虽然可能有更简单的方法,但我相信类似的方法应该会奏效:

var childIds = children.Select(c => c.Id).ToArray();
Parent parent = null;
Child child = null;

return Session
    .QueryOver.Of<Child>(() => child)
    .WhereRestrictionOn(x => x.Id).IsIn(childIds)
    .JoinQueryOver(x => x.Parents, () => parent)
    .Where(Restrictions.Eq(Projections.Count(() => parent.Id), childIds.Length))
    .Select(Projections.Group(() => parent.Id));
var childIds=children.Select(c=>c.Id.ToArray();
Parent=null;
Child=null;
返回会话
(()=>子项)的.QueryOver
.WhereRestrictionOn(x=>x.Id).IsIn(childId)
.JoinQueryOver(x=>x.parent,()=>parent)
.Where(Restrictions.Eq(projects.Count(()=>parent.Id),childId.Length))
.选择(projects.Group(()=>parent.Id));
你看过我的答案(或其主题的变体)是否适合你吗?如有反馈,将不胜感激。
return from child in Db.Children.Where(c => children.Contains(c))
       from parent in child.Parents
       group parent by parent.Id into g
       where g.Count() == children.Length
       select g.Single();
var childIds = children.Select(c => c.Id).ToArray();
Parent parent = null;
Child child = null;

return Session
    .QueryOver.Of<Child>(() => child)
    .WhereRestrictionOn(x => x.Id).IsIn(childIds)
    .JoinQueryOver(x => x.Parents, () => parent)
    .Where(Restrictions.Eq(Projections.Count(() => parent.Id), childIds.Length))
    .Select(Projections.Group(() => parent.Id));