Linq distinct - Count

I want to execute a query in a list of sample 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 a list of dates with a number of unique user logins.

For example:

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

I tried to test several linq statements, but none of them give me the desired result. The closest I have gives me excellent dates, but with an account of all users.

Any help would be greatly appreciated.

+59
c # linq
Feb 03 2018-11-11T00: 00Z
source share
5 answers
 logins .GroupBy(l => l.Date) .Select(g => new { Date = g.Key, Count = g.Select(l => l.Login).Distinct().Count() }); 
+112
Feb 03 '11 at 9:45
source share

I understand that this is an ancient question, but I came across it and saw a comment about the desire for the syntax of the method and could not answer it ... I may have a coding disorder.

In query syntax, it looks like this: note that for Distinct and Count

no query syntax.
 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() }; 

To compare side by side with the original method syntax (which I personally like better) here you go ...

 logins .GroupBy(l => l.Date) .Select(g => new { Date = g.Key, Count = g.Select(l => l.Login).Distinct().Count() }); 
+15
Dec 18 '14 at 21:32
source share

Can be performed within a single GroupBy call,

  var Query = list.GroupBy( (item => item.DateTime), (key, elements) => new { key = key, count = elements .Distinct() .Count() } ); 
+7
Feb 20 '13 at 12:52
source share

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() }); 
+5
Feb 03 2018-11-11T00:
source share

Another way to solve this problem 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(); 
+1
Feb 03 '11 at 10:25
source share



All Articles