【问题标题】:Nhibernate Subquery - Could not resolve property of type xNhibernate 子查询 - 无法解析 x 类型的属性
【发布时间】:2013-06-07 17:49:26
【问题描述】:

我需要一些帮助来确定应该基于一对多关系进行过滤的查询。我有一个表“产品”和“产品类型”。其中“产品”有很多“产品类型”。这些模型运行良好,我只是不知道如何根据“ProductType”记录过滤“Product”记录。

在此示例中,我想选择所有“产品”,其中 Product.Name == "somename" && Product.Version == "9.6"。我想在 ProductType.Type == "someType" 的地方过滤这些结果。 以下是抛出异常:

无法解析属性:ProductType of: my.namespace.ProductType

我正在使用 Ayende 博客中的示例:Query Examples

 var product = _session.CreateCriteria<Product>()
                              .Add(Restrictions.Eq("Name", "somename"))
                              .Add(Restrictions.Eq("Version", "9.6"))
                              .Add(Subqueries.PropertyIn("Id",
                                                         DetachedCriteria.For<ProductType>()
                                                                         .SetProjection(Projections.Property("Product.Id"))
                                                                         .CreateCriteria("ProductType")
                                                                         .Add(Restrictions.Eq("Type", "someType"))
                                       )).List<Product>().SingleOrDefault();

我接近了吗?有人可以给我一些帮助吗?谢谢!

编辑

这让我很接近。如果我删除第二个 CreateCriteria,我会得到一个填充了 Types 的产品。一旦我将加入添加回来.. 我得到 0 个结果。

 var products = _session.CreateCriteria("Product")
                                   .Add(Restrictions.Eq("Name", "somename"))
                                   .Add(Restrictions.Eq("Version", "9.6")) //This works
                                   .CreateCriteria("Types", "productTypes",   JoinType.LeftOuterJoin)
                                   .Add(Restrictions.Eq("Type", "typename")).List<Product>();

            return products.FirstOrDefault();

【问题讨论】:

    标签: c# sql nhibernate orm fluent-nhibernate


    【解决方案1】:

    非常接近 - 您只需告诉 CreateCriteria 如何获取 ProductType。例如:

    var product = _session.CreateCriteria<Product>()
        .Add(Restrictions.Eq("Name", "somename"))
        .Add(Restrictions.Eq("Version", "9.6"))
        .Add(Subqueries.PropertyIn("Id",
             DetachedCriteria.For<ProductType>()
            .SetProjection(Projections.Property("Product.Id"))
            .CreateCriteria("Product.ProductType", JoinType.InnerJoin)
            .Add(Restrictions.Eq("Type", "someType"))
         ))
         .List<Product>()
         .SingleOrDefault();
    

    【讨论】:

    • 奇怪的是我遇到了同样的问题。
    • 其实.. 现在它抱怨无法解析属性:Product.Id of:{namespace}
    • 您能否使用 Product 和 ProductType 类以及您使用的映射来更新问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    相关资源
    最近更新 更多