Linq to nhibernate - 其中集合包含具有 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 to nhibernate 查询来选择一个父对象,其中包含具有特定 id 的子对象。 返回x => x.Children.Contains
不起作用。我也尝试过这个。
return x => (from y in x.Children where y.ChildId.Equals(childId) select y).Count() > 0
我的流畅映射如下所示
HasManyToMany<Child>(x => x.Children)
.Table("ParentsChildren")
.ParentKeyColumn("ParentId")
.ChildKeyColumn("ChildId");
如何通过 id 找到包含子项的父项?
I have 2 objects like this
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; }
}
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");
How can I find the parent that contains a child by id?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是哪个 NHibernate 版本?
如果您使用新的 NHibernate linq 库,那么我认为您会做类似的事情:
在旧版本中,您必须使用
.Linq()
而不是.Query; ()
:我不记得旧的 NHibernate linq 库是否已经支持此类查询。
Which NHibernate version are you using?
If you are using the new NHibernate linq library, then I think you van do something like:
In older versions you had to use
.Linq<T>()
instead of.Query<T>()
:I can't remember if the older NHibernate linq library already supported these kind of queries.