Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从数组中找到最接近DateTime的较大时间。现在_C#_Linq_Parsing_Datetime - Fatal编程技术网

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()呼叫。这里完全没有必要。