我有一个 List<IOhlcv>
我需要得到 string[]
的 DateTime
来自 List
的字符串:
public interface ITick
{
DateTimeOffset DateTime { get; }
}
public interface IOhlcv : ITick
{
decimal Open { get; set; }
decimal High { get; set; }
decimal Low { get; set; }
decimal Close { get; set; }
decimal Volume { get; set; }
}
//candles is a List<IOhlcv>
var candles = await importer.ImportAsync("FB");
这里有什么?
string[] x = from p in candles
orderby p.DateTime ascending
select What goes here?
我可以得到一个List
的 Datetime
也像这样:
var sd = candles.Select(i => i.DateTime).ToList();
有没有办法转换 List<DateTime> to a List<String>
没有循环?
我知道我可以做这样的事情,但我正在努力避免循环:
List<string> dateTimeStringList = new List<string>();
foreach (var d in candles)
dateTimeStringList.Add(d.DateTime.ToString());
return dateTimeStringList ;
最佳答案
Is there a way to convert the
List<DateTime>
to aList<String>
without looping?
这就是您可以使用 Linq Select 实现的方式:
List<DateTime> list = new List<DateTime>();
list.Add(DateTime.Now);
var format = "yyyy MMMMM dd";
var stringList = list.Select(r => r.ToString(format)).ToList();
你可以替换format
以上为你的最爱DateTime format .
关于c# - 将 IEnumerable<Class> 转换为 List<string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541324/