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.
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();
Have you tried:
List<int> newList = originalList.Where(v => v != null)
.Select(v => v.Value)
.ToList();
?
Try:
var numbers1 = new List<int?>() { 1, 2, null};
var numbers2 = numbers1.Where(n => n.HasValue).Select(n => n.Value).ToList();
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