I'm trying to execute the follow code and it works fine if myObject is != null but throws a "value cannot be null" error if myObject is null. Ideally, if myObject is null then I'd like to just have the HashSet have a single value of 0.
var ids = new HashSet<int>(myObject.Select(p => p.ID));
I've tried a couple different things. I'm not sure why I thought this would work.
var ids = new HashSet<int>(myObject.Select(p => p.ID).Where(p => p != null));
and this which seemed like it should work.
var ids = new HashSet<int>(myObject.Select(p => (p == null) ? 0 : p.ID));
Finally this worked but it seems like there's got to be a better way.
var ids = new HashSet<int>(myObject!= null ? myObject.Select(p => p.ID) : new int[] {0});
Anyone have a better way to do this?