I have an element type:
public class FieldInfo
{
public string Label { get; set; }
public string Value { get; set; }
}
And I have an array filled with FieldInfo objects.
FieldInfo[] infos = new FieldInfo[]
{
new FieldInfo{Label = "label1", Value = "value1"},
new FieldInfo{Label = "label2", Value = "value2"}
};
Now I want to convert that array to a new one that contains following values:
string[] wantThatArray = new string[] {"label1", "value1", "label2", "value2"};
Is there a short way to do the conversion from an array like infos to an array like wantThatArray?
Maybe with LINQ's Select?
string[] wantThatArray = infos
.SelectMany(f => new[] {f.Label, f.Value})
.ToArray();
I would keep it simple:
string[] wantThatArray = new string[infos.Length * 2];
for(int i = 0 ; i < infos.Length ; i++) {
wantThatArray[i*2] = infos[i].Label;
wantThatArray[i*2 + 1] = infos[i].Value;
}
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