I have:
IEnumerable<string> c = codigo_incidencia.Split('#');
I need to cast "c" to be an IEnumerable<int>
. I don´t know how to do this cast in C#.
Can someone help me?
I have:
IEnumerable<string> c = codigo_incidencia.Split('#');
I need to cast "c" to be an IEnumerable<int>
. I don´t know how to do this cast in C#.
Can someone help me?
Shortest way is to using linq .Select
likewise:
var c = codigo_incidencia.Split('#').Select(int.Parse);
If you are not sure that the sections are valid ints then you'd want to use a TryParse
as in: Select parsed int, if string was parseable to int. And if working with C# 7.0 you can look at this answer of the question:
var result = codigo_incidencia.Split('#')
.Select(s => new { Success = int.TryParse(s, out var value), value })
.Where(pair => pair.Success)
.Select(pair => pair.value);
Use LINQ:
IEnumerable<int> c = codigo_incidencia.Split('#').Select(x => int.Parse(x));
You can do it like this if the strings are always guaranteed to be numbers:
IEnumerable<int> c = codigo_incidencia.Split('#').Select(stringValue => int.Parse(stringValue));