如何:
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