也許這樣的事情?
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()
});
請加你想這些LINQ語句 - 然後我們就可以明白爲什麼他們錯在哪裏。 – 2011-02-03 09:46:08