2011-02-16 82 views
2

我創建了一個winform應用程序,我使用Nhibernate。在我的應用程序中,我需要選擇我的數據庫中的所有產品(74000行)。查詢在Nhibernate

在我的產品對象上,我有一個對象列表,稱爲條形碼。

我要選擇我的所有產品,但只應裝滿物品放在產品操作搜索條碼列表中,如果barcodetype = 20,組識別符號= 4

我不知道如何寫這個查詢,我一直在嘗試與分離的標準,但我不能得到它的工作。任何人都可以幫我編寫查詢,或者鏈接到一個頁面,我可以看到它的完成情況?

+1

爲什麼你需要選擇所有74,000行? – Phill 2011-02-16 20:36:53

+0

我需要它用於導出方法。 – 2011-02-16 20:45:30

回答

3

由於集合不能「半載」,因此需要投影。

我這是怎麼通常做到這一點:

var result = session.CreateQuery(@" 
    select product, barcode 
    from Product product 
    join product.Barcodes barcode 
    where barcode.Barcodetype = 20 
    and barcode.Groupid = 4 
    ") 
    .List<object[]>() 
    .ToLookup(x => (Product)x[0], x => (Barcode)x[1]); 

(我假定你有標性質稱爲Barcodetype和羣ID,因爲你沒有指定任何關係,所有產品都有至少一個這種類型的條形碼)

0

而不是實例化所有產品對象及其條形碼的一部分,如何使用Criteria對條形碼進行過濾,通過所提及的類型/組進行過濾並加入到父產品中。

然後使用AliasToBeanResultTransformer填充簡單DTO對象的列表。

+0

或使用無狀態會話... – asgerhallas 2011-02-17 14:49:02

0

我找到了答案,我做了一個外部左連接 crit.CreateCriteria(「barcodeses」,「bc」,SqlCommand.JoinType.LeftOuterJoin).Add(Expression.Or(Expression .Eq(「bc.Groupid」,CType(cbBarcodeGroup.SelectedItem,Domain.Barcodegroup).ID),Expr ession.IsNull(「bc.Groupid」)))