c# - Merge two IEnumerables in elegant way -
i have 2 ienumerable variables, both can null. need merge them single list. here direct approach.
var ienumerable1 = getenumerable1(); var ienumerable2 = getenumerable2(); if(ienumerable1 != null){ if(ienumerable2 != null){ return ienumerable1.union(ienumerable2); } return ienumerable1; } else{ return ienumerable2; }
is there more elegant way in less lines of code this?
just check null , assign enumerable.empty
if null. can done in 1 step null coalescing operator ??
var ienumerable1 = getenumerable1() ?? enumerable.empty<whatevertype>(); var ienumerable2 = getenumerable2() ?? enumerable.empty<whatevertype>(); return ienumerable1.union(ienumerable2);
Comments
Post a Comment