你先看一下,假如有需要補充說明的地方跟我說
另外,建議下一次能把測試資料轉成Json或是csv,讓有心的回答者方便測試
void Main()
{
#region 測試資料
var json = @"
[
{
""No"": ""01"",
""Name"": ""香蕉"",
""City"": ""台中"",
""Town"": ""霧峰"",
""Create_Time"": ""2018/11/24 12:00:00""
},
{
""No"": ""02"",
""Name"": ""蘋果"",
""City"": ""台中"",
""Town"": ""霧峰"",
""Create_Time"": ""2018/11/20 12:00:00""
},
{
""No"": ""03"",
""Name"": ""冰梨"",
""City"": ""台中"",
""Town"": ""霧峰"",
""Create_Time"": ""2018/11/15 12:00:00""
},
{
""No"": ""04"",
""Name"": ""番茄"",
""City"": ""南投"",
""Town"": ""草屯"",
""Create_Time"": ""2018/11/23 12:00:00""
},
{
""No"": ""05"",
""Name"": ""輯麴"",
""City"": ""南投"",
""Town"": ""草屯"",
""Create_Time"": ""2018/10/01 12:00:00""
},
{
""No"": ""06"",
""Name"": ""冰蜜桃 "",
""City"": ""南投"",
""Town"": ""草屯"",
""Create_Time"": ""2018/10/18 12:00:00""
},
{
""No"": ""07"",
""Name"": ""西衣"",
""City"": ""苗栗"",
""Town"": ""大湖"",
""Create_Time"": ""2018/11/05 12:00:00""
},
{
""No"": ""08"",
""Name"": ""草莓"",
""City"": ""苗栗"",
""Town"": ""大湖"",
""Create_Time"": ""2018/11/24 12:00:00""
},
{
""No"": ""09"",
""Name"": ""芭樂"",
""City"": ""苗栗"",
""Town"": ""大湖"",
""Create_Time"": ""2018/10/9 12:00:00""
}
]
";
#endregion
var testDatas = JsonConvert.DeserializeObject<List<Test>>(json);
var result = testDatas.GroupBy(g => new { g.City, g.Town })
.Select(s => new { s.Key, Datas = s.Where(w2 => w2.Create_Time == s.Max(max => max.Create_Time)).First() })
.Select(s => new { s.Datas.No,s.Datas.Name,s.Key.City, s.Key.Town,s.Datas.Create_Time});
}
class Test
{
public string No { set; get; }
public string Name { set; get; }
public string City { set; get; }
public string Town { set; get; }
public DateTime Create_Time { set; get; }
}