LINQ的不同 - 伯爵 [英] Linq distinct - Count

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

问题描述

我在找对象的示例列表

执行查询

 日期用户名01/01/2011詹姆斯
01/01/2011杰米
01/01/2011亚历克斯
01/01/2011詹姆斯
02/01/2011亚光
02/01/2011杰米
02/01/2011亚历克斯
02/01/2011詹姆斯
02/01/2011詹姆斯
02/01/2011露西
02/01/2011亚历克斯
03/01/2011詹姆斯
03/01/2011鲍勃
03/01/2011鲍勃
03/01/2011詹姆斯
03/01/2011詹姆斯
亚历克斯04/01/2011
亚历克斯04/01/2011
亚历克斯04/01/2011

我想使用LINQ查询日期列表具有独特的用户登录次数。

例如:

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

我曾尝试为测试了一些LINQ语句,但没有这些都让我想要的结果。我有最接近的是给了我不同的日期,但与所有用户的计数。

任何帮助将大大AP preciated。


解决方案

 登录
  .GroupBy(L => l.Date)
  。选择(G =>新
  {
    日期= g.Key,
    数= g.Select(L => l.Login).Distinct()。COUNT()
  });

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

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

For example:

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

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.

Any help would be greatly appreciated.

解决方案

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

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

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