c# - Linq to nhibernate - 其中集合包含具有 id 的对象

标签 c# linq nhibernate fluent-nhibernate linq-to-nhibernate

我有 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 的子项的父项。 return 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 找到包含子项的父项?

最佳答案

您使用的是哪个 NHibernate 版本?

如果您正在使用新的 NHibernate linq 库,那么我认为您可以执行以下操作:

var parent = session.Query<Parent>()
    .Where(p => p.Children.Any(c => c.ChildId == childId))
    .FirstOrDefault();

在旧版本中,您必须使用 .Linq<T>()而不是 .Query<T>() :

var parent = session.Linq<Parent>()
    .Where(p => p.Children.Any(c => c.ChildId == childId))
    .FirstOrDefault();

我不记得旧的 NHibernate linq 库是否已经支持这些类型的查询。

关于c# - Linq to nhibernate - 其中集合包含具有 id 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4739129/

相关文章:

c# - 快速截取窗口屏幕截图的替代方法

c# - WPF应用程序类和主窗口初始化

c# - 在 NHibernate 中调用 Named Query 并将参数传递给 Sybase Server

c# - 使用 Nhibernate Criteria Api 查询集合?

c# - 使用 Visual Basic.NET (VB.NET) 开发的 ASP.NET Core 应用程序可能吗? VS中没有模板

c# - 我如何将值存储到 href 链接 asp.net c# 中的多个值

c# - Linq 查询返回记录列表而不是显示单个记录

c# - 如何使用 C# 读取包含数组的 json 文件并对其执行 LINQ 查询?

c# - 在 Linq 中处理 HashSet 时我应该使用 Except 还是 Contains

nhibernate - 使用 nhibernate(和 queryover)急切地获取多个嵌套关联