Search code examples
c#intnullable

Convert List<int?> to List<int>


Suppose, I have a list of Nullable Integer's & I want to convert this list into List<int> which contains only values.

Can you please help me to solve this.


Solution

  • Filter out the null values, get the numbers using Value property and put them into a list using ToList:

    yourList.Where(x => x != null).Select(x => x.Value).ToList();
    

    You can also use Cast

    yourList.Where(x => x != null).Cast<int>().ToList();