如何使用NHibernate的条件api和实体查询子属性,以仅加载与谓词条件匹配的子属性 [英] How to query a subproperty with NHibernate’s criteria api and the entity to load only the subproperties matching a predicate condition

查看:81
本文介绍了如何使用NHibernate的条件api和实体查询子属性,以仅加载与谓词条件匹配的子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下内容:

public class Order
{
   public virtual int OrderId {get;set}
   public virtual ISet<Product> Products {get;set}
}

public class Product
{
   public virtual int ProductId {get;set}
   public virtual string ProductName {get;set}
}

您将如何使用条件api进行查询,以便仅返回具有特定订单ID的订单,并且其产品集合也应过滤到名称以字母L开头的产品?

How would you query using the criteria api so that only an order with a specific orderid is returned and its Product collection should also be filtered down to Products whose Name start with the lettter P?

推荐答案

我会使用DetachedCriteria来解决这个问题:

I would go about this with a DetachedCriteria:

DetachedCriteria crit = DetachedCriteria.For<Order>();

crit.Add(Restrictions.Eq("OrderId",orderID);
crit.CreateCriteria("Products","products");
crit.Add(Restrictions.Like("products.ProductName","P%");

crit.List();

然后执行条件并获得结果.

and then executing the criteria and getting the results.

这篇关于如何使用NHibernate的条件api和实体查询子属性,以仅加载与谓词条件匹配的子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆