使用LINQ进行分组的聚合。

huangapple go评论99阅读模式
英文:

Linq an aggregate with a grouping

问题

这是您的查询的翻译:

  1. var topUser = (from p in _context.Sessions
  2. where p.SDate > DateTime.Now.AddYears(-1)
  3. join c in _context.Users on p.UsrId equals c.UsrId
  4. group p by p.UsrId into grouped
  5. let count = grouped.Count()
  6. orderby count descending
  7. select new
  8. {
  9. UsrID = grouped.Key,
  10. FirstName = grouped.FirstOrDefault().User.FirstName,
  11. LastName = grouped.FirstOrDefault().User.LastName,
  12. sCount = count
  13. })
  14. .Take(5);

希望这可以帮助您解决问题。

英文:

I'm translating a query and here's the original:

  1. select top 5 t.usrID, u.FirstName, u.LastName, t.cnt as sCount
  2. from (
  3. select usrID, COUNT(rID) as cnt
  4. from sessions as s where s.sDate > DATEADD(yy, -1, getdate())
  5. group by usrID
  6. ) as t
  7. inner join users as u on t.usrID = u.usrID
  8. order by t.cnt desc

Here's what I have so far:

  1. var topUser = (from p in _context.Sessions
  2. where p.SDate > DateTime.Now.AddYears(-1)
  3. join c in _context.Users on p.UsrId equals c.UsrId into j1
  4. from j2 in j1.DefaultIfEmpty()
  5. // group j2 by p.UsrId into grouped
  6. select new
  7. {
  8. p.UsrId,
  9. j2.FirstName,
  10. j2.LastName,
  11. cnt = p.RId
  12. })
  13. //.OrderBy(d => d.cnt)
  14. //.GroupBy(o => o.UsrId)
  15. .Take(5);

I'm having trouble figuring out how to include count() and group by clauses. When I include groupBy my other columns disappear. Thank you.

答案1

得分: 1

这是对你的回答的回答 - 不是对你最初的查询的回答。我会将其放在注释中,但是没有格式很难解释。

假设User对象具有Session的集合,你的第一个语句可以大大简化:

  1. var topUsers = _context.Sessions
  2. .Where(s => s.SDate > DateTime.Now.AddYears(-1))
  3. .Select(s => new
  4. {
  5. s.UsrId,
  6. cnt = s.User.Sessions.Count(u => u.UsrId == s.UsrId)
  7. })
  8. .OrderByDescending(s => s.cnt)
  9. .Take(5);

你可以塑造结果以获得一个ViewModel,其中还包括FirstName和LastName。这一切都归结为定义具有一对多关系的模型。

英文:

This is the answer to your answer - not to your original query. I would put it as comment, but without formatting it's hard to explain
Assuming User object has collection of Session your first statement can be drastically simplified:

  1. var topUsers = _context.Sessions
  2. .Where(s => s.SDate > DateTime.Now.AddYears(-1))
  3. .Select(s => new
  4. {
  5. s.UsrId,
  6. cnt = s.User.Sessions.Count(u => u.UsrId == s.UsrId)
  7. })
  8. .OrderByDescending(s => s.cnt)
  9. .Take(5);

You can shape the results to get a ViewModel that also has FirstName and LastName. It all boils down to defining a model with one-to-many relationship

答案2

得分: 0

以下是翻译好的代码部分:

  1. 好的,虽然它不是最高效的,但它可以工作:
  2. var topUsers = _context.Sessions
  3. .Where(s => s.SDate > DateTime.Now.AddYears(-1))
  4. .GroupBy(s => s.UsrId)
  5. .Select(ws => new { ws.Key, cnt = ws.Count() })
  6. .OrderByDescending(s => s.cnt)
  7. .Take(5);
  8. var topNamedUsers = topUsers.Join(_context.Users, ws => ws.Key, ud => ud.UsrId, (ws, ud) => new { ws.Key, ud.FirstName, ud.LastName, ws.cnt });
英文:

Alright so it's not the most efficient but it works:

  1. var topUsers = _context.Sessions
  2. .Where(s => s.SDate > DateTime.Now.AddYears(-1))
  3. .GroupBy(s => s.UsrId)
  4. .Select(ws => new { ws.Key, cnt = ws.Count() })
  5. .OrderByDescending(s => s.cnt)
  6. .Take(5);
  7. var topNamedUsers = topUsers.Join(_context.Users, ws => ws.Key, ud => ud.UsrId, (ws, ud) => new { ws.Key, ud.FirstName, ud.LastName, ws.cnt });

答案3

得分: 0

var topUser = (from p in _context.Sessions
where p.SDate > DateTime.Now.AddYears(-1)
join c in _context.Users on p.UsrId equals c.UsrId into j1
from j2 in j1.DefaultIfEmpty()
group j2 by p.UsrId into g
select new
{
UsrId = g.Key,
FirstName = g.Select(x => x.FirstName).FirstOrDefault(),
LastName = g.Select(x => x.LastName).FirstOrDefault(),
sCount = g.Count()
})
.OrderByDescending(d => d.sCount)
.Take(5);

英文:
  1. var topUser = (from p in _context.Sessions
  2. where p.SDate > DateTime.Now.AddYears(-1)
  3. join c in _context.Users on p.UsrId equals c.UsrId into j1
  4. from j2 in j1.DefaultIfEmpty()
  5. group j2 by p.UsrId into g
  6. select new
  7. {
  8. UsrId = g.Key,
  9. FirstName = g.Select(x => x.FirstName).FirstOrDefault(),
  10. LastName = g.Select(x => x.LastName).FirstOrDefault(),
  11. sCount = g.Count()
  12. })
  13. .OrderByDescending(d => d.sCount)
  14. .Take(5);

huangapple
  • 本文由 发表于 2023年1月6日 13:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定