C# 如何将此HQL转换为DetachedCriteria?

C# 如何将此HQL转换为DetachedCriteria?,c#,nhibernate,hibernate,orm,hql,C#,Nhibernate,Hibernate,Orm,Hql,此HQL语句在执行时产生以下结果: select t, count(s) from Submission s right join s.Topics as t GROUP BY t.Id result[0] [0] topic_id, topic_name, ... [1] 10 result[1] [0] topic_id, topic_name, ... [1] 12 . result[n] [0] topic_id, topic_n

此HQL语句在执行时产生以下结果:

select t, count(s) from Submission s right join s.Topics as t GROUP BY t.Id

result[0]
    [0] topic_id, topic_name, ... 
    [1] 10

result[1]
    [0] topic_id, topic_name, ... 
    [1] 12
     .
result[n]
    [0] topic_id, topic_name, ... 
    [1] 19
此DetachedCriteriaAPI产生几乎相似的结果,但不加载主题

ProjectionList PrjList = Projections.ProjectionList();
PrjList.Add(Projections.GroupProperty("Topics"), "t");
PrjList.Add(Projections.Count("Id"));

DetachedCriteria Filter = DetachedCriteria.For<Submission>();
Filter.CreateCriteria("Topics", "t", JoinType.RightOuterJoin);
Filter.SetProjection(PrjList);

result[0]
    [0] null
    [1] 10

result[1]
    [0] null
    [1] 12
     .
result[n]
    [0] null
    [1] 19
ProjectionList PrjList=Projections.ProjectionList();
PrjList.Add(Projections.GroupProperty(“主题”),“t”);
PrjList.Add(Projections.Count(“Id”));
DetachedCriteria筛选器=DetachedCriteria.For();
CreateCriteria(“主题”、“t”、JoinType.RightOuterJoin);
Filter.SetProjection(PrjList);
结果[0]
[0]空
[1] 10
结果[1]
[0]空
[1] 12
.
结果[n]
[0]空
[1] 19

出于某种原因,nhibernate拒绝为结果集创建主题对象,但它为HQL查询创建主题对象。为什么会这样?

看看Ayende博客上的分离标准,它为您提供了一个非常详细的关于左侧外部联接和按HQL分组的分步指南,以及后面的映射

var orderIDsContainingCurrentSku = DetachedCriteria.For<OrderItem>()
    .Add<OrderItem>(x=>x.Product.SKU==sku)
    .SetProjection(Projections.Property("Order.id"));
var skusOfProductsAppearingInOrdersContainingCurrentSku = DetachedCriteria.For<OrderItem>()
    .SetProjection(Projections.GroupProperty("Product.id"))
    .AddOrder(NHibernate.Criterion.Order.Desc(Projections.Count("Order.id")))
    .Add<OrderItem>(x=>x.Product.SKU!=sku)
    .Add(Subqueries.PropertyIn("Order.id", orderIDsContainingCurrentSku))
    .SetMaxResults(15);
var recommended = _session.CreateCriteria<Product>()
    .SetFetchMode<Product>(x => x.Descriptors, FetchMode.Join)
    .Add(Subqueries.PropertyIn("id", skusOfProductsAppearingInOrdersContainingCurrentSku))
    .SetResultTransformer(Transformers.DistinctRootEntity)
    .List<Product>();

看看是怎么做的

您正在尝试将GroupProperty设置为类,但不是属性。恐怕您应该将主题中的每个属性分组到ProjectionList。在本例中,我将创建一个方法扩展,将调用类的每个属性添加到grouping中。使用HQL查询,NHibernate能够完成我在这里尝试的工作。我只是想找到使用CriteriaAPI做同样事情的方法。但是,你的建议需要进行转换,我希望避免这种情况。请在这里发布相关代码,链接可能会断开(特别是像你使用的链接,这是一个带有偏移量的列表页面)。
SELECT this_.SKU                 as SKU1_1_,
   this_.ProductName         as ProductN2_1_1_,
   this_.BasePrice           as BasePrice1_1_,
   this_.WeightInPounds      as WeightIn4_1_1_,
   this_.DateAvailable       as DateAvai5_1_1_,
   this_.EstimatedDelivery   as Estimate6_1_1_,
   this_.AllowBackOrder      as AllowBac7_1_1_,
   this_.IsTaxable           as IsTaxable1_1_,
   this_.DefaultImageFile    as DefaultI9_1_1_,
   this_.AmountOnHand        as AmountO10_1_1_,
   this_.AllowPreOrder       as AllowPr11_1_1_,
   this_.DeliveryMethodID    as Deliver12_1_1_,
   this_.InventoryStatusID   as Invento13_1_1_,
   descriptor2_.SKU          as SKU3_,
   descriptor2_.DescriptorID as Descript1_3_,
   descriptor2_.DescriptorID as Descript1_4_0_,
   descriptor2_.Title        as Title4_0_,
   descriptor2_.Body         as Body4_0_
FROM   Products this_
   left outer join ProductDescriptors descriptor2_
     on this_.SKU = descriptor2_.SKU
WHERE  this_.SKU in (SELECT   top 15 this_0_.SKU as y0_
FROM     OrderItems this_0_
WHERE    not (this_0_.SKU = 'Binoculars2' /* @p0 */)
    and this_0_.OrderID in (SELECT this_0_0_.OrderID as y0_
        FROM   OrderItems this_0_0_
        WHERE  this_0_0_.SKU = 'Binoculars2' /* @p1 */)
            GROUP BY this_0_.SKU
            ORDER BY count(this_0_.OrderID) desc)