Table
.GroupBy(x=>x.Name)
.Select(x=>new {Name=x.Key,
Entrytime=x.Min(m=>m.Entrytime),
Exittime=x.Max(m=>m.Exittime)})
这是一个 LinqPad 查询,展示了当 Entrytime 和 Exittime 是字符串而不是日期时间时如何在 IEnumerable 上执行此操作:
var list= new[]{
new {Name="A",Entrytime="10:30 AM",Exittime="6:30PM"},
new {Name="A",Entrytime="7:30 AM",Exittime="5:30PM"},
new {Name="B",Entrytime="10:30 AM",Exittime="2:30PM"},
new {Name="A",Entrytime="5:30 AM",Exittime="3:30PM"},
new {Name="B",Entrytime="11:30 AM",Exittime="4:30PM"},
new {Name="A",Entrytime="8:30 AM",Exittime="7:30PM"},
new {Name="C",Entrytime="9:30 AM",Exittime="1:30PM"},
new {Name="C",Entrytime="10:30 AM",Exittime="9:30PM"}};
var result=list.GroupBy(x=>x.Name)
.Select(x=>new {Name=x.Key,
Entrytime=x.Min(m=>DateTime.Parse(m.Entrytime)).ToString("h:mm tt"),
Exittime=x.Max(m=>DateTime.Parse(m.Exittime)).ToString("h:mm tt")});
result.Dump();