如何:
private class Test
{
public string Category { get; set; }
public DateTime PostDate { get; set; }
public string Post { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
List<Test> test = new List<Test>();
test.Add(new Test() { Category = "A", PostDate = new DateTime(2010, 5, 5, 12, 0, 0), Post = "A1" });
test.Add(new Test() { Category = "B", PostDate = new DateTime(2010, 5, 5, 13, 0, 0), Post = "B1" });
test.Add(new Test() { Category = "A", PostDate = new DateTime(2010, 5, 6, 12, 0, 0), Post = "A2" });
test.Add(new Test() { Category = "A", PostDate = new DateTime(2010, 5, 6, 13, 0, 0), Post = "A3" });
test.Add(new Test() { Category = "A", PostDate = new DateTime(2010, 5, 6, 14, 0, 0), Post = "A4" });
var q = test.GroupBy(t => t.Category).Select(g => new { grp = g, max = g.Max(t2 => t2.PostDate).Date }).SelectMany(x => x.grp.Where(t => t.PostDate >= x.max));
}
来源
2010-05-29 22:02:46
Luc
此代码似乎只拉取类别中具有等于该类别的最大日期的类别中的第一项。问题是要求类别中的所有项目都是该类别的最大计数日期。 – 2010-05-29 21:49:19
好点...我更新了我的答案 – 2010-05-29 22:03:50
事实证明,你的代码已经关闭了,但我的阅读需求也是如此。尽管在我自己的批评中出现了错误,但第二次做得很好。 – 2010-05-29 22:14:36