I have this code:
var contractsID = contracts.Select(x => x.Id);
int?[] contractsIDList = contractsID.ToArray();//for debug
In this line:
int?[] contractsIDList = contractsID.ToArray();//for debug
I get this error:
Can not implicitly convert type int[] to int
what i try to do is to make contractsIDList Nullable type.
How to make int array Nullable?
The error you should get is:
Can not implicitly convert type int[] to int?[]
Thus you need to convert the values:
int?[] contractsIDList = contractsId.Cast<int?>().ToArray();//for debug
Arrays are always reference types - so they're already nullable.
But i guess that you actually want to get an int?[]
from an int[]
(because the Id
is not nullable). You can use Array.ConvertAll
:
int[] contractsID = contracts.Select(x => x.Id).ToArray();
int?[] contractsIDList = Array.ConvertAll(contractsID, i => (int?)i);
or cast it directly in the LINQ query:
int?[] contractsIDList = contracts.Select(x => (int?) x.Id).ToArray();
The easiest way in your case is to get int?
from the Select
:
var contractsID = contracts.Select(x => (int?)x.Id);
int?[] contractsIDList = contractsID.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With