我在内存中处理每日股票市场数据的 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/