C# 如何从数组中找到最接近DateTime的较大时间。现在
我正在努力寻找最接近当前时间的更大时间。 这是我的代码:C# 如何从数组中找到最接近DateTime的较大时间。现在,c#,linq,parsing,datetime,C#,Linq,Parsing,Datetime,我正在努力寻找最接近当前时间的更大时间。 这是我的代码: var busTimes = new string[] { "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00" } .Select(x => DateTime.Parse(x)) .ToList(
var busTimes = new string[]
{
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"20:00",
"21:00"
}
.Select(x => DateTime.Parse(x))
.ToList();
var now = DateTime.Now.TimeOfDay;
var closestTime = (from x in busTimes
where x.busTimes.TimeOfDay > now
orderby x.busTimes.TimeOfDay ascending
select x).First();
但我得到了这个错误:
错误CS1061“DateTime”不包含“Bustimmes”和的定义
没有扩展方法“bastimes”接受类型为“DateTime”的第一个参数
可以找到(是否缺少using指令或程序集引用?)
如何修复此问题?
x
是一个日期时间
,正如错误消息所说,它没有属性
var closestTime = (from x in busTimes
where x.TimeOfDay > now
orderby x.TimeOfDay ascending
select x).First();
x
是一个DateTime
,正如错误消息所说,它没有bastimes
属性
var closestTime = (from x in busTimes
where x.TimeOfDay > now
orderby x.TimeOfDay ascending
select x).First();
您的查询应该是:
var closestTime = (from x in busTimes
where x.TimeOfDay > now
orderby x.TimeOfDay ascending
select x).First();
由于x
标识符表示DateTime
,而DateTime
结构没有'bastimes'属性。您的查询应该是:
var closestTime = (from x in busTimes
where x.TimeOfDay > now
orderby x.TimeOfDay ascending
select x).First();
由于x
标识符表示DateTime
,而DateTime
结构没有'bastimes'属性。我将去掉.ToList()代码>呼叫。这里完全没有必要。我要去掉.ToList()代码>呼叫。这里完全没有必要。