Linq distinct - Count

前端 未结 5 575
一向
一向 2020-11-29 01:16

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/201         


        
相关标签:
5条回答
  • 2020-11-29 01:44

    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

    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()
      });
    
    0 讨论(0)
  • 2020-11-29 01:53

    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();
    
    0 讨论(0)
  • 2020-11-29 01:56

    Can be done within single GroupBy call,

      var Query = list.GroupBy(
                     (item => item.DateTime),
                     (key, elements) => new  { 
                                              key = key,
                                              count = elements
                                                      .Distinct()
                                                      .Count()
                                             }
                     );
    
    0 讨论(0)
  • 2020-11-29 02:04

    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() 
                });
    
    0 讨论(0)
  • 2020-11-29 02:06
    logins
      .GroupBy(l => l.Date)
      .Select(g => new
      {
        Date = g.Key,
        Count = g.Select(l => l.Login).Distinct().Count()
      });
    
    0 讨论(0)
提交回复
热议问题