Linq to SQL GROUP 和 SUM

Linq to SQL GROUP and SUM

我有这个 linq to sql 工作(在这个小组的帮助下)

var resultTotal = (from fuf in db.fad_userFoods
              join fu in db.fad_user on fuf.userID equals fu.userID
              join ff in db.fad_food on fuf.foodID equals ff.foodID 
              where fuf.userID == thisGuid && fuf.quantityAmount >= ff.portionSize
              group fuf.userID by new {fu.dateJoined} into g
              select new AccountHomeViewModel
              {
                totalPercent = (g.Count() / (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5.0)) * 100,
                totalPortionsPossible = (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now)+1)*5,
                totalPortionsAchieved = g.Count()
              }).First();

我现在想在我的 "select new AccountHomeViewModel" 中得到一些计算值。

所以我将 linq 修改为 sql 因此:

var resultTotal = (from fuf in db.fad_userFoods
              join fu in db.fad_user on fuf.userID equals fu.userID
              join ff in db.fad_food on fuf.foodID equals ff.foodID 
              where fuf.userID == thisGuid && fuf.quantityAmount >= ff.portionSize
              group fuf.userID by new {fu.dateJoined, fuf.quantityAmount, ff.portionSize} into g
              select new AccountHomeViewModel
              {
                totalPercent = (g.Count() / (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5.0)) * 100,
                totalPortionsPossible = (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now)+1)*5,
                totalPortionsAchieved = g.Count(),
                calcValue = g.Sum(g.Key.quantityAmount)/g.Sum(g.Key.portionSize)
              }).First();

但我在 Visual Studio 2015 年的两个 g.Sum 部分都收到智能感知错误:

Argument 2: cannot convert from 'decimal?' to 'System.Func<System.Guid?, int>'

所以我可以看到这是因为我按 fuf.userID 分组,这是一个 GUID。但为什么这会影响我的 SUM 但不会影响我的 COUNT?

我知道这是我的理解不足(这是我的第一个 MVC 应用程序和 linq-to-sql)但是我应该去哪里纠正这个问题?

提前致谢。

您只是从 group by 中带回 fuf.userID,同时还对您分组依据的 属性 求和。因此,任何在 select 的聚合函数中访问小数 quantityAmount 的尝试都将失败。尝试:

var resultTotal = (from fuf in db.fad_userFoods
          join fu in db.fad_user on fuf.userID equals fu.userID
          join ff in db.fad_food on fuf.foodID equals ff.foodID 
          where fuf.userID == thisGuid && fuf.quantityAmount >= ff.portionSize
          group new {fuf.userID, fuf.quantityAmount, ff.portionSize} by new {fu.dateJoined} into g
          select new AccountHomeViewModel
          {
            totalPercent = (g.Count() / (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5.0)) * 100,
            totalPortionsPossible = (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now)+1)*5,
            totalPortionsAchieved = g.Count(),
            calcValue = g.Sum(q => q.quantityAmount)/g.Sum(q => q.portionSize)
          }).First();

这是 LINQ,应该会产生您需要的 T-SQL(根据下面的评论):

var resultTotal = (from fap in db.fad_user_physician
          join fu in db.fad_user on fup.userID equals fu.userID
          join fuf in db.fad_userFoods on fu.userID equals fuf.userID
          join ff in db.fad_food on fuf.foodID equals ff.foodID 
          where fup.physicianID == thisGuid && fuf.quantityAmount >= ff.portionSize
          group new {fuf.quantityAmount, ff.portionSize, ff.alwaysOnePortion} by new {fuf.userId, fu.dateJoined} into g
          select new AccountHomeViewModel
          {
            totalPercent = (g.Count() / (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5.0)) * 100,
            calcValue = g.Sum(q => q.alwaysOnePortion == true ? 1: q.quantityAmount/q.portionSize)
          }).First();

您可以尝试使用 .

   var resultTotal = (from fuf in db.fad_userFoods
      join fu in db.fad_user on fuf.userID equals fu.userID
      join ff in db.fad_food on fuf.foodID equals ff.foodID 
      where fuf.userID == thisGuid && fuf.quantityAmount >= ff.portionSize
      group new {Uid=fuf.userID,Qa=fuf.quantityAmount,Ps=ff.portionSize} by new {fu.dateJoined} into g
      select new AccountHomeViewModel
      {
        totalPercent = (g.Count() / (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5.0)) * 100,
        totalPortionsPossible = (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now)+1)*5,
        totalPortionsAchieved = g.Count(),
        calcValue = g.Select(d=>d.Qa).Sum()/g.Select(d=>d.Ps).Sum()
      }).First();

编辑 尝试将 calcValue 更改为

    calcValue = g.Select(d=>d.Qa).Sum()/g.Select(d=>(d.Qa>d.Ps?d.Qa:d.Ps)).Sum()

希望对您有所帮助。