0
我有這樣的數據庫結構:在NHibernate中選擇沒有多對多關係的項目?
Products
ProductId
Categories
CategoryId
ProductsInCategories
ProductId
CategoryId
我需要找到所有沒有在一個類別的產品。現在,我使用此代碼:
var results = Session
.CreateCriteria<Product>()
.List<Product>()
.Where(product=> !product.Categories.Any())
.ToList();
因此,我返回數據庫中的所有產品,然後過濾它們。這是低效的,我需要一個更好的方法。
我試過這段代碼:
var res = Session.QueryOver<Product>()
.Left.JoinQueryOver(product=> product.Categhories)
.Where(categories => !categories.Any())
.TransformUsing(Transformers.DistinctRootEntity)
.List();
但它並沒有在所有的工作。我嘗試了一些變化,但是也沒有奏效。
我該如何使用NHibernate執行此查詢?
謝謝,做它。這個API使用起來有點棘手! – Oliver 2013-02-20 13:17:05