I currently have:

List<TimeSpan> times = new List<TimeSpan>();
   // ... setup the thousands of times ...
string[] timeStrings = new string[times.Count];
for (int i = 0; i < times.Count; i++)
   timeStrings[i] = times[i].ToString("mm.ss");

I feel like there should be an easy way to do this in LINQ, but I can't find it. I got close with times.Select(s => s.ToString("mm.ss").ToArray()), but it just got the first element.

Side note: Are there any good LINQ tutorials out there?

有帮助吗?

解决方案

You almost had it:

var timesAsString = times.Select(s => s.ToString("mm.ss")).ToArray()

其他提示

var timesAsString = times.Select(t => t.ToString("mm.ss")).ToArray();

Your ToArray call is currently on the string, not the enumerable.

This is basically right, the problem is that your ToArray is being called on the string when it should be outside of that (basically a typo);

What you have;

times.Select(s => s.ToString("mm.ss").ToArray())

what you should have;

times.Select(s => s.ToString("mm.ss")).ToArray();
times.Select(s => s.ToString("mm.ss")).ToArray();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top