我有一个带有日期属性的对象列表,并且使用Linq,我想返回最新的日期。
普通C#代码。
List<ProjectMetric> A= _projectMetric.AggregateProjectMetrics;
_measure = A[0].Measures.GetCurrentMeasure();
string m = _measure.DateMeasured.ToString("dd-MMM-yy");
我尝试使用OrderByDescending函数使用以下内容获取最新日期,但没有成功:
IEnumerable<ProjectMetric> lm = A.OrderByDescending(s => s.Measures.GetCurrentMeasure());
_measure=lm.First().Measures.GetCurrentMeasure();
if (_measure != null)
{
string m = _measure.DateMeasured.ToString("dd-MMM-yy");
tblCell.InnerText = m;
}
在这里,我得到列表中第一项的日期,因为我做了A [0]。我做错了什么?
答案 0 :(得分:0)
要获得最高日期,请使用:
DateTime maxDate = _projectMetric
.AggregateProjectMetrics
.OrderByDescending(x => x.DateMeasured)
.Select(x => x.DateMeasured)
.First();
答案 1 :(得分:0)
var maxDate = A[0].Measures.Max(x => x.DateMeasured);
答案 2 :(得分:-1)
是的,这里有一些伪代码:
HighestDate = _projectMetric.AggregateProjectMetrics.OrderByDescending(x => x.DateMeasured).Select(x => x.DateMeasured).FirstOrDefault()
或
HighestDate = (from x in _projectMetric.AggregateProjectMetrics OrderBy x.DateMeasured descending select x.DateMeasured).FirstOrDefault();