2010-06-11 57 views
0

這是我遇到的問題。 我有一個超過100條記錄的數據庫。 我正在通過數據分頁來一次獲得9個結果。 當我添加一個檢查項目是否處於活動狀態時,它會導致結果開始翻倍。需要幫助,以正確刪除NHibernate中的重複項

一點背景: 「產品」是實際產品線 「ProductSkus」是存在於產品線 當有產品中更多的則1 ProductSku實際產品,它會導致返回重複的條目。

請參見下面的NHibernate的查詢:

result = this.Session.CreateCriteria<Model.Product>()     
       .Add(Expression.Eq("IsActive", true)) 
       .AddOrder(new Order("Name", true)) 
       .SetFirstResult(indexNumber).SetMaxResults(maxNumber) 

       // This part of the query duplicates the products 
       .CreateAlias("ProductSkus", "ProdSkus", JoinType.InnerJoin) 
       .Add(Expression.Eq("ProdSkus.IsActive", true)) 

       .CreateAlias("ProductToSubcategory", "ProdToSubcat") 
       .CreateAlias("ProdToSubcat.ProductSubcategory", "ProdSubcat") 
       .Add(Expression.Eq("ProdSubcat.ID", subCatId)) 

       // This part takes out the duplicate products - Removes too many items... 
       // Turns out that with .SetFirstResult(indexNumber).SetMaxResults(maxNumber) 
       // it gets 9 records back then the duplicates are removed. 
       // Example: 
       //  Total Records over 100 
       //  Max = 9 
       //  4 Duplicates removed 
       //  Yields 5 records when there should be 9 
       // Why??? This line is ran in NHibernate on the data after it has been extracted from the SQL server. 
       .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer()) 

       .List<Model.Product>(); 

我加入了DistinctRootEntityResultTransformer清理重複。問題是它將9個記錄拉回來,其中包含重複項。 DistinctRootEntityResultTransformer然後清理9條記錄中的重複項。我基本上需要一個獨特的語句才能在SQL服務器上運行。

但是,不同的SQL不會起作用,因爲NHibernate默認情況下希望從語句的選擇部分中的每個表中添加每個字段。我只使用屬於根表的字段來開始(Model.Product)。如果我可以告訴NHibernate不要將連接的表中的字段添加到語句的選擇部分並添加Distinct,它就可以工作。

我用NHibernare Profiler來看看實際的查詢:

SELECT top 9 this_.ID       as ID351_3_, 
      this_.Name      as Name351_3_, 
      this_.Description     as Descript3_351_3_, 
      this_.IsActive     as IsActive351_3_, 
      this_.ManufacturerID    as Manufact5_351_3_, 
      prodskus1_.ID      as ID373_0_, 
      prodskus1_.Description   as Descript2_373_0_, 
      prodskus1_.PartNumber    as PartNumber373_0_, 
      prodskus1_.Price     as Price373_0_, 
      prodskus1_.IsKit     as IsKit373_0_, 
      prodskus1_.IsActive    as IsActive373_0_, 
      prodskus1_.IsFeaturedProduct  as IsFeatur7_373_0_, 
      prodskus1_.DateAdded    as DateAdded373_0_, 
      prodskus1_.Weight     as Weight373_0_, 
      prodskus1_.TimesViewed   as TimesVi10_373_0_, 
      prodskus1_.TimesOrdered   as TimesOr11_373_0_, 
      prodskus1_.ProductID    as ProductID373_0_, 
      prodskus1_.OverSizedBoxID   as OverSiz13_373_0_, 
      prodtosubc2_.ID     as ID362_1_, 
      prodtosubc2_.MasterSubcategory as MasterSu2_362_1_, 
      prodtosubc2_.ProductID   as ProductID362_1_, 
      prodtosubc2_.ProductSubcategoryID as ProductS4_362_1_, 
      prodsubcat3_.ID     as ID352_2_, 
      prodsubcat3_.Name     as Name352_2_, 
      prodsubcat3_.ProductCategoryID as ProductC3_352_2_, 
      prodsubcat3_.ImageID    as ImageID352_2_, 
      prodsubcat3_.TriggerShow   as TriggerS5_352_2_ 
FROM  Product this_ 
    inner join ProductSku prodskus1_ 
     on this_.ID = prodskus1_.ProductID 
      and (prodskus1_.IsActive = 1) 
    inner join ProductToSubcategory prodtosubc2_ 
     on this_.ID = prodtosubc2_.ProductID 
    inner join ProductSubcategory prodsubcat3_ 
     on prodtosubc2_.ProductSubcategoryID = prodsubcat3_.ID 
WHERE this_.IsActive = 1 /* @p0 */ 
    and prodskus1_.IsActive = 1 /* @p1 */ 
    and prodsubcat3_.ID = 3 /* @p2 */ 
ORDER BY this_.Name asc 

如果我手動修改查詢並直接在SQL服務器,我得到的結果集我要上運行它(我刪除了所有多餘的字段選擇部分並添加DISTINCT):

SELECT DISTINCT top 9 this_.ID    as ID351_3_, 
      this_.Name      as Name351_3_, 
      this_.Description     as Descript3_351_3_, 
      this_.IsActive     as IsActive351_3_, 
      this_.ManufacturerID    as Manufact5_351_3_, 
FROM  Product this_ 
    inner join ProductSku prodskus1_ 
     on this_.ID = prodskus1_.ProductID 
      and (prodskus1_.IsActive = 1) 
    inner join ProductToSubcategory prodtosubc2_ 
     on this_.ID = prodtosubc2_.ProductID 
    inner join ProductSubcategory prodsubcat3_ 
     on prodtosubc2_.ProductSubcategoryID = prodsubcat3_.ID 
WHERE this_.IsActive = 1 /* @p0 */ 
    and prodskus1_.IsActive = 1 /* @p1 */ 
    and prodsubcat3_.ID = 3 /* @p2 */ 
ORDER BY this_.Name asc 

最大的問題我現在必須問的是...

必須我在NHibernate的查詢改變什麼最終得到電子xact相同的結果?

在此先感謝。

回答