Reputation: 7746
I have a List<IOhlcv>
and I need to get the string[]
of DateTime
strings from the 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");
What goes here?:
string[] x = from p in candles
orderby p.DateTime ascending
select What goes here?
I can get a List
of Datetime
like this also:
var sd = candles.Select(i => i.DateTime).ToList();
Is there a way to convert the List<DateTime> to a List<String>
without looping?
I know I can do something like this but I am trying to avoid loops:
List<string> dateTimeStringList = new List<string>();
foreach (var d in candles)
dateTimeStringList.Add(d.DateTime.ToString());
return dateTimeStringList ;
Upvotes: 2
Views: 350
Reputation: 2999
Is there a way to convert the
List<DateTime>
to aList<String>
without looping?
This is how you can do it with 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();
You can replace format
above to your favorite DateTime format.
Upvotes: 1
Reputation: 179
What about this:
string[] x = from p in candles
orderby p.DateTime ascending
select p.DateTime.ToString()
You're right, that last one won't. I removed it.
Upvotes: 0