2013-03-25 30 views
0

我有一個像NHibernate的查詢所有條件

Product {List<Cost> Costs} 

Cost{List<Invoice> Invoices, Product product} 

Invoice{bool isIncluded} 

表結構需要一個查詢來獲取這對於其中包括無發票的任何費用(isIncluded = false爲所有)

我所有產品想是這樣:

Product pro= null; 
Product p = null; 

     var costQuery = QueryOver.Of<Cost>() 
      .JoinAlias(c => c.Product,() => p) 
      .Where(() => p.Id == pro.Id)     
      .WhereNot(c=>c.Invoices.Any(i=>i.IsIncluded)) 
      .Select(c => c.Id); 

     var query = CurrentSession.QueryOver<Product>(() => pro)     
            .WithSubquery.WhereExists(costQuery); 

使用的 '任意' 在查詢錯誤了:

無法識別的方法調用:System.Linq.Enumerable:布爾 不限[發票](System.Collections.Generic.IEnumerable 1[Trigger.StageGate.Services‌​.BusinessEntities.Invoice], System.Func 2 [Trigger.StageGate.Services.BusinessEntities.Invoice,System.Boolean])

+0

「錯誤」?這是你得到的異常消息或編譯器錯誤嗎?我不這麼認爲。請發佈實際消息。 – 2013-03-25 11:29:24

+0

exception msg 無法識別的方法調用:System.Linq.Enumerable:Boolean任何[發票](System.Collections.Generic.IEnumerable'1 [Trigger.StageGate.Services.BusinessEntities.Invoice],System.Func'2 [Trigger.StageGate .Services.BusinessEntities.Invoice,System.Boolean]) – user1147738 2013-03-25 11:34:06

回答

0

嘗試:

var costQuery = QueryOver.Of<Cost>() 
     .JoinAlias(c => c.Product,() => p) 
     .Where(() => p.Id == pro.Id)    
     .JoinQueryOver<Invoice>(c => c.Invoices) 
     .WhereNot(i => i.IsIncluded)  
     .Select(c => c.Id); 
+0

查詢將返回具有isIncluded爲false的任何發票的所有成本,而我正在查找沒有isIncluded爲true的發票的成本。假設成本c1包含發票I1(isInclude = true),I2(= false),I3(= false)。成本c2有發票I4(=假),I5(=假)。成本c3具有發票I6(= false)。我只需要花費c2和c3,而查詢將返回所有三個c1 c2 c3。 – user1147738 2013-03-25 13:12:24

+0

已將查詢更新爲 成本成本=空; 成本cst = null; var invoiceQuery = QueryOver.Of () .JoinAlias(i => i.Cost,()=> cost) .Where(()=> cost.Id == cst.Id) 。Where(i = > i.IsIncluded) 。選擇(c => c.Id); (()=> p.Id == pro.Id) var costQuery = QueryOver.Of ((=> cst) .JoinAlias(c => c.Product,()=> p) .WithSubquery.WhereNotExists(invoiceQuery) .Select(c => c.Id); 這工作,但不知道如果它是最好的方式。 – user1147738 2013-03-25 13:34:33

+0

我不認爲如果沒有子查詢,你可以真的做到這一點,並且QueryOver不支持可枚舉的擴展方法(如Any,Contains等),你必須使用QueryOver方法 – 2013-03-25 13:41:01