1

我有这段代码(不确定它是否有效,因为我无法测试它):

var test = base.UnitOfWork.Session.Query<NutritionFact>()
                                  .Where(x => x.NutritionalServing.Id == servingId)
                                  .GroupBy(x => x.UserVerifiedFacts)
                                  .OrderByDescending(x => x.Sum(e => e.UserVerifiedFacts.Count()))
                                  .Take(3)
                                  .Select(r => new
                                      {
                                          c = r.Key,
                                          Sum = r.Sum(x => x.UserVerifiedFacts.Count())
                                      })
                                  .ToList();

我想做的是找到所有NutritionFacts正确的servingId. 然后,我想为每个NutritionFacts发现的用户计算验证信息的用户数。然后我想获得前 3 个结果并使用它们。

我现在所做的会导致“未实现”错误,这显然是因为 nhibernate sum 只能处理任何参数。

在此处输入图像描述

4

1 回答 1

2

我会尝试:

var test = ...Query<NutritionFact>()
             .Where(x => x.NutritionalServing.Id == servingId)
             .Select(x=> new { Nf= x, VfCount= x.UserVerifiedFacts.Count() })
             .OrderByDescending(x => x.VfCount))
             .Take(3)
             .ToList();

你不需要使用Sum. Sum用于聚合(汇总)分组列,但您只需要相关Count()表中每个特定的行数NuturitionFact

它对应于这个 SQL 语句(我想这就是你需要的)

SELECT TOP 3 n.*, 
       (SELECT COUNT(*) FROM UserVerifiedFact uv WHERE uv.NuturitionFacts_Id = n.Id) AS NumbOfVer  
       FROM NutritionFact n 
       WHERE n.NutritionalServing_Id = @servingId
       ORDER BY NumbOfVer DESC
于 2013-02-05T23:06:15.900 回答