var acts = new List<MyClass>(new[] {
new MyClass { Date = DateTime.Parse("01/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("01/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("01/01/2011"), acty = "alex" },
new MyClass { Date = DateTime.Parse("01/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("02/01/2011"), acty = "matt" },
new MyClass { Date = DateTime.Parse("02/01/2011"), acty = "jamie" },
new MyClass { Date = DateTime.Parse("02/01/2011"), acty = "alex" },
new MyClass { Date = DateTime.Parse("02/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("02/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("02/01/2011"), acty = "lucy" },
new MyClass { Date = DateTime.Parse("02/01/2011"), acty = "alex" },
new MyClass { Date = DateTime.Parse("03/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("03/01/2011"), acty = "bob" },
new MyClass { Date = DateTime.Parse("03/01/2011"), acty = "bob" },
new MyClass { Date = DateTime.Parse("03/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("03/01/2011"), acty = "james" },
new MyClass { Date = DateTime.Parse("04/01/2011"), acty = "alex" },
new MyClass { Date = DateTime.Parse("04/01/2011"), acty = "alex" },
new MyClass { Date = DateTime.Parse("04/01/2011"), acty = "alex" }
});
要从列表中获取不同的值,我使用以下代码:
var dacts = acts。选择(o =比;o.acty) .Distinct () .ToList ();
我需要一些帮助,根据活动计数降序排序数据。
我已经查看了Linq distinct - Count,它与我的要求不同。
上面的问题得到一列的不同值和另一列的不同计数。
但是,在我的例子中,我希望列的不同值按照同一列的计数排序
所以,我希望我的答案是…
james, alex, bob, jamie, lucy, matt
var dacts = acts.Select(o => o.acty).Distinct().OrderByDescending().ToList();
试试这个
var distinctNames = acts.GroupBy(m => m.acty).Select(grp => new { name = grp.Key, count = grp.Count() }).OrderByDescending(k => k.count).ToList();