gpt4 book ai didi

c# - 如何在 xml 数据的 linq 查询中使用 TryParse?

转载 作者:可可西里 更新时间:2023-11-01 08:21:03 28 4
gpt4 key购买 nike

我在内存中处理每日股票市场数据的 xml,我得到其中一个日期的值“8/221/19055”。我看到 TryParse 可能是我检查有效日期的最佳选择,但 MSDN 文档似乎对第二个参数“out DateTime result”的解释很清楚。如何在下面的 linq 查询中使用它?

var makeInfo =
from s in doc.Descendants("quote")
where s.Element("LastTradeDate") != null
&& s.Attribute("symbol") != null
let dateStr = s.Element("LastTradeDate").Value
where !string.IsNullOrEmpty(dateStr)
&& DateTime.Parse(dateStr, enUS) == targetDate
select new DailyPricingVolDP((string)s.Attribute("symbol"),
(DateTime)s.Element("LastTradeDate"),
(double)s.Element("Open"),
(double)s.Element("DaysHigh"),
(double)s.Element("DaysLow"),
(double)s.Element("LastTradePriceOnly"),
(long)s.Element("Volume"));

最佳答案

Func<string, DateTime?> tryToGetDate =
value =>
{
DateTime dateValue;
return DateTime.TryParse(value, out dateValue) ? (DateTime?) dateValue : null;
};

var makeInfo =
from s in doc.Descendants("quote")
where s.Element("LastTradeDate") != null
&& s.Attribute("symbol") != null
let dateStr = s.Element("LastTradeDate").Value
let dateValue = tryToGetDate(dateStr)
where dateValue != null && (DateTime)dateValue == targetDate
select .... etc etc

关于c# - 如何在 xml 数据的 linq 查询中使用 TryParse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9003697/

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