gpt4 book ai didi

c# - 从DateTime的列表中找到最高的DateTime

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:14 25 4
gpt4 key购买 nike

我的问题是,我想从DateTime列表中找到最高的DateTime吗?

我有一个数组,假设string[] btime = new string[100];

在该数组中,我存储了来自SQL Server的日期
SQL查询为[CONVERT(varchar(10),GETDATE(),101)],它以MM / dd / yyyy格式返回日期
然后将日期与自己指定的时间连接起来
.btime[j] = SqlServerDate + " " + 15:20;等;

现在,从这个给定的数组中,我想找到最高的日期和时间

所以,我使用了这种逻辑

string large=""
large=btime[0];

for (int i = 0; i < j; i++)
{
if (DateTime.Compare(DateTime.Parse(btime[i]),DateTime.Parse(large)) > 0)
{
large = btime[i];
}
}


但我在错误

if(DateTime.Compare(DateTime.Parse(btime[i]),DateTime.Parse(large)) > 0)


错误为字符串,无法识别为有效的DateTime,因为我的系统DateTime格式为yyyy / dd / MM,所以发生了此错误

Plz的任何人都可以帮助我解决这个问题
我不想更改系统格式

最佳答案

其他人提出了解析DateTime的不同方法。对我来说,这似乎毫无意义-如果您可以更改查询,则只需避免首先转换为字符串。您使用的转换次数越少,发生此类问题的机会就越少。

更改查询,以得到DateTime值,然后在LINQ中找到最新的查询是微不足道的:

DateTime latest = dateTimes.Max();

关于c# - 从DateTime的列表中找到最高的DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5776841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com