I\'m using a linq query to output an int array. But I need to pass this into a method that only accepts int?[].
So after searching on ways to convert int[] to int?[] I
int?[]
is an array of int?
. All you need is change lambda in Select
, to return an int?
:
int?[] vids2 = new[] { "", "1", "2", "3" }
.Where(x => !String.IsNullOrWhiteSpace(x))
.Select(x => (int?)Convert.ToInt32(x))
.ToArray();
If you already have an int[]
, you can use Cast()
to cast the elements to int?
int[] ints = { 1, 2, 3 };
int?[] nullableInts = ints.Cast().ToArray();