nhibernate map一个类别包含前n个产品

时间:2012-04-10 02:16:57

标签: nhibernate nhibernate-mapping

我有一个问题,那就是“类别类”包含很多产品类。但现在,我有一个CategoryService类,它将在同一个sutiation中实现查找IList,每个类别只包含前n个产品,而不是所有产品,我该怎么办?给我一个选择,谢谢!

代码清单:

 public class category
 {
    public int Id{get;set;}
    public int Categoryname{get;set;}
    public IList<Product> Products{get;set;}
 }

public class Product
{
   public int Id{get;set;}
   public string ProductName{get;set;}
   public Category Category{get;set;}
   etc...
}

然后有域名服务:

public class CategoryService
{
   private ICategoryRepository categoryRepository;

   public CategoryService(ICategoryRepository categoryRepository)
   {
     this.categoryRepository=categoryRepository;
   }

   public IList<Category> FindAll()
   {
     IList<Category> categories;
     categories=categoryRepository.FindAll();

     //and now i need categoryRepository find all category ,and every category contains top n product, what should i do;

   return categories;
   }
 }

1 个答案:

答案 0 :(得分:0)

var count = // it's your top
session.QueryOver<Category>
.Fetch(category => category.Product).Eager
.Take(count)
.TransformUsing(Transformers.DistinctRootEntity)
.List();
相关问题