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屋!
查看全文