简体   繁体   English

Linq 不同 - 计数

[英]Linq 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.我想使用 linq 查询具有唯一用户登录数的日期列表。

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.我已经尝试了许多 linq 语句,但这些语句都没有给我想要的结果。 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()
  });

I realize this is an ancient question but I ran across it and saw the comment about wanting method syntax and couldn't help myself to answer it... I may have a coding disorder.我意识到这是一个古老的问题,但我遇到了它并看到了关于想要方法语法的评论,无法帮助自己回答......我可能有编码障碍。

In query syntax it looks like this... note that there is no query syntax for Distinct and Count在查询语法中,它看起来像这样...请注意, DistinctCount没有查询语法

from l in logins
group l by l.Date into g
select new
{
    Date = g.Key,
    Count = (from l in g select l.Login).Distinct().Count() 
};

For a side by side comparison to the original method syntax (which personally I like better) here you go...为了与原始方法语法(我个人更喜欢)进行并排比较,你去......

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

Can be done within single GroupBy call,可以在单个 GroupBy 调用内完成,

  var Query = list.GroupBy(
                 (item => item.DateTime),
                 (key, elements) => new  { 
                                          key = key,
                                          count = elements
                                                  .Distinct()
                                                  .Count()
                                         }
                 );

Something like this maybe?也许像这样的东西?

var list = new List<MyClass>(new[] {
        new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "alex" },
        new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "matt" },
        new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "jamie" },
        new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "alex" },
        new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "lucy" },
        new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "alex" },
        new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "bob" },
        new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "bob" },
        new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "james" },
        new MyClass { Date = DateTime.Parse("04/01/2011"), Username = "alex" },
        new MyClass { Date = DateTime.Parse("04/01/2011"), Username = "alex" },
        new MyClass { Date = DateTime.Parse("04/01/2011"), Username = "alex" }
    });

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

Another way to solve this is to group twice, check the sample解决这个问题的另一种方法是分组两次,检查样本

          var dist = listLogins.GroupBy(d => d.date + d.Username)
              .Select(x => x.First())
              .GroupBy(d => d.date).Select(y => new { date = y.Key, count = y.Count() }).ToList();

I don't think there is need of Distinct count in Oryol's answer.我认为在 Oryol 的回答中不需要 Distinct 计数。 The groupup automatically picks distinct keys and it can be accessed as g.key groupup 会自动选择不同的键,它可以作为 g.key 访问

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM