考虑简单的类
public class Data
{
public DateTime Date;
public object Content;
}
现在我有一个名为IEnumerable<Data>
的{{1}},我希望Linq能够对每天的旧项目进行排序。如果有同一天的项目,我只对当天的最新项目感兴趣。
Linq可以实现吗?
答案 0 :(得分:4)
以下是如何做到这一点:
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = true;
processInfo.WindowStyle = ProcessWindowStyle.Maximized;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
为了更具可读性,您还可以在var result =
datas
//Group by day (we don't include time here)
.GroupBy(x => x.Date.Date)
//For each group (day), get most recent item (we include time here)
.Select(g => g.OrderByDescending(x => x.Date).First())
.ToList();
声明中使用x.TimeOfDay
代替x.Date
。
答案 1 :(得分:2)
使用此:
var result = from x in datas
group x by x.Date.Date into g
select g.OrderByDescending(x => x.Date).FirstOrDefault();
或者
var result = datas.GroupBy(x => x.Date.Date)
.Select(g => g.Where(x => x.Date == g.Max(y => y.Date))
.First());