LINQ到NHibernate的 - 如果集合包含对象ID为 [英] Linq to nhibernate - Where collection contains object with id
问题描述
我有2个对象,像这样
public class Child
{
public virtual int ChildId { get; set; }
}
public class Parent
{
public virtual int ParentId { get; set; }
public virtual IList<Child> Children { get; set; }
}
我想写一个LINQ NHibernate的查询来选择父在哪里它包含具有特定ID的孩子。 返回X => x.Children.Contains
不起作用。我也试过这种
I am trying to write a linq to nhibernate query to select a parent where it contains a child with a specific id. return x => x.Children.Contains
does not work. I also tried this.
return x => (from y in x.Children where y.ChildId.Equals(childId) select y).Count() > 0
我流利的映射看起来像这样
My fluent mapping looks like this
HasManyToMany<Child>(x => x.Children)
.Table("ParentsChildren")
.ParentKeyColumn("ParentId")
.ChildKeyColumn("ChildId");
我如何才能找到,通过ID包含一个孩子的家长吗?
How can I find the parent that contains a child by id?
推荐答案
这NHibernate的版本,您使用的?
Which NHibernate version are you using?
如果您使用的是新的NHibernate的LINQ库,然后我想你面包车做这样的事情:
If you are using the new NHibernate linq library, then I think you van do something like:
var parent = session.Query<Parent>()
.Where(p => p.Children.Any(c => c.ChildId == childId))
.FirstOrDefault();
在旧版本,你必须使用 .Linq< T>()
,而不是 .Query< T>()
:
In older versions you had to use .Linq<T>()
instead of .Query<T>()
:
var parent = session.Linq<Parent>()
.Where(p => p.Children.Any(c => c.ChildId == childId))
.FirstOrDefault();
我不记得,如果老NHibernate的LINQ库已经支持这些类型的查询。
I can't remember if the older NHibernate linq library already supported these kind of queries.
这篇关于LINQ到NHibernate的 - 如果集合包含对象ID为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!