c# - 将 IEnumerable<Class> 转换为 List<string>

标签 c# casting ienumerable

我有一个 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?

我可以得到一个ListDatetime也像这样:

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 a List<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/

相关文章:

c# - 添加带有 Identity asp.net core 的 UserClaim

c# - Linq - 局部不同

c# - 访问 IEnumerable 中包含的项目的属性

c# - Linq 查询到 orderby 级别,然后是父项和子项

c# - Xamarin.Forms 输入单元格如何更改占位符的字体大小

c# - 比较转换为对象的值类型

c - 未初始化指针的可能输出

c++ - 指针之间的转换运算符

c# - 仅打印出 IEnumerable 中的奇数元素?

c# - 什么是 C# 中的 IEnumerable 接口(interface)?如果我们不使用它怎么办?