下面是一些代碼,做你想做的。它獲取每個ID的最新狀態並忽略完成的記錄。我提供了一個可以運行的完整工作示例(並且希望能夠適應您的真實數據)。
//the example data provided by the OP
var data = new []
{
new { Record = 1, Id = "A", State = "Waiting", Date = new DateTime(2013, 1, 1) },
new { Record = 2, Id = "A", State = "InProgress", Date = new DateTime(2013, 1, 2) },
new { Record = 3, Id = "A", State = "Finished", Date = new DateTime(2013, 1, 3) },
new { Record = 4, Id = "B", State = "Waiting", Date = new DateTime(2013, 1, 1) },
new { Record = 5, Id = "C", State = "Waiting", Date = new DateTime(2013, 1, 1) },
new { Record = 6, Id = "C", State = "InProgress", Date = new DateTime(2013, 1, 2) },
new { Record = 7, Id = "D", State = "Waiting", Date = new DateTime(2013, 1, 1) },
new { Record = 8, Id = "D", State = "InProgress", Date = new DateTime(2013, 1, 2) },
};
var query = from d in data
//put the newest record first
orderby d.Date descending
//group by the id
group d by d.Id into groupedById
//get the latest record for each id
let latest = groupedById.First()
//filter out finished records
where latest.State != "Finished"
select latest;
這裏是LinqPad的輸出。
你會發現,我們有最新的狀態,對每個項目,除了因爲它完成其已被忽略「A」。
你到目前爲止嘗試過什麼?向我們展示你在分組排序和最大化方面所做的工作,並且我們可以告訴你哪裏出錯了。對於學習來說,不僅僅是獲得解決方案更好...... – Chris