There is a struct S with 2 string fields: A and B.
I want to convert an array of S into string array, containing all non-empty unique As and Bs. What is the most efficient way for that?
Regards,
var myArray = S.Select( x => new [] { x.A, x.B })
.SelectMany( x => x)
.Where( x=> !string.IsNullOrEmpty(x))
.Distinct()
.ToArray();
Above only works if the unique constraint is on the resulting collection - if you need a unique constraint on the set of A's and B's the following would work:
var As = S.Select(x => x.A)
.Where( x=> !string.IsNullOrEmpty(x))
.Distinct();
var Bs = S.Select(x => x.B)
.Where( x=> !string.IsNullOrEmpty(x))
.Distinct();
var myArray = new[] { As, Bs }.SelectMany(x => x).ToArray();
var myArray = As.Concat(Bs).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