将 IEnumerable<Class> 转换为 List<string>
Cast IEnumerable<Class> to List<string>
我有一个 List<IOhlcv>
,我需要从 List
:
中获取 DateTime
个字符串中的 string[]
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?
我也可以像这样得到 Datetime
的 List
:
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 ;
这个怎么样:
string[] x = from p in candles
orderby p.DateTime ascending
select p.DateTime.ToString()
你是对的,最后一个不会。我删除了它。
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。
我有一个 List<IOhlcv>
,我需要从 List
:
DateTime
个字符串中的 string[]
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?
我也可以像这样得到 Datetime
的 List
:
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 ;
这个怎么样:
string[] x = from p in candles
orderby p.DateTime ascending
select p.DateTime.ToString()
你是对的,最后一个不会。我删除了它。
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。