NHibernate 标准集合包含 [英] NHibernate Criteria Collection Contains
本文介绍了NHibernate 标准集合包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个映射到多对多集合的父/子关系.
I have a parent/child relationship mapped with a many-to-many set.
public class Parent
{
public ISet<Child> Children { get; set; }
}
public class Child {}
public class ParentMap : ClassMap<Parent>
{
HasManyToMany(x => x.Children)
.AsSet();
}
如何编写查询以选择包含给定子项的所有父项?我会猜到它会是这样的,但是这个 API 不存在:
How can I write a query to select all of the parents that contain a given child? I would have guessed that it would be something like this, but this API does not exist:
Session.CreateCriteria<Parent>()
.Add(Expression.Contains("Children", child)
.List<Parent>();
我一生都无法在任何地方找到答案.我的大脑今天没有完全发挥作用,而 Google 到目前为止也让我失望.
I can't for the life of me find the answer anywhere. My brain is not fully functioning today and Google has so far failed me.
推荐答案
这样的事情怎么样?
Session.CreateCriteria<Parent>()
.CreateCriteria("Children")
.Add(Expression.Eq("Id", child.Id)
.List<Parent>();
或
Session.CreateCriteria<Parent>()
.CreateCriteria("Children")
.Add(Expression.In("Id", child.Id)
.List<Parent>();
所以你可以传入一个 ID 数组.
so you can pass in an array of Ids.
这篇关于NHibernate 标准集合包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文