Linq 不同 - 计数 [英] Linq distinct - Count

查看:27
本文介绍了Linq 不同 - 计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对示例对象列表执行查询

I am looking to perform a query on an example list of objects

Date     Username

01/01/2011 james
01/01/2011 jamie
01/01/2011 alex
01/01/2011 james
02/01/2011 matt
02/01/2011 jamie
02/01/2011 alex
02/01/2011 james
02/01/2011 james
02/01/2011 lucy
02/01/2011 alex
03/01/2011 james
03/01/2011 bob
03/01/2011 bob
03/01/2011 james
03/01/2011 james
04/01/2011 alex
04/01/2011 alex
04/01/2011 alex

我想使用 linq 查询具有唯一用户登录次数的日期列表.

I want to use linq to query the list of dates with the number of unique user logins.

例如:

01/01/2011 - 3
02/01/2011 - 5
03/01/2011 - 2
04/01/2011 - 1

我已经尝试了许多 linq 语句,但都没有给我想要的结果.我得到的最接近的是给我不同的日期,但要计算所有用户.

I have tried as tested a number of linq statements but none of these are giving me the desired result. The closest I have got is giving me the distinct dates but with a count of all the users.

任何帮助将不胜感激.

推荐答案

logins
  .GroupBy(l => l.Date)
  .Select(g => new
  {
    Date = g.Key,
    Count = g.Select(l => l.Login).Distinct().Count()
  });

这篇关于Linq 不同 - 计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆