Reputation: 1452
Is there a LINQ statemant to transfer a List of a class like
private class ToSelectMany
{
public string SelectMe1 { get; set; }
public string SelectMe2 { get; set; }
public string IgnoreMeString { get; set; }
public bool IgnoreMeBool { get; set; }
}
to an array like
[0] "list element 1 - SelectMe1"
[1] "list element 1 - SelectMe2"
[2] "list element 2 - SelectMe1"
[3] "list element 2 - SelectMe2"
[4] "list element 3 - SelectMe1"
[5] "list element 3 - SelectMe2"
...
I tried
List<ToSelectMany> elements = GetElements();
string[] elementArray = elements.SelectMany(a => a.ToSelect1, b => b.ToSelect2);
and some similar things, but it won't work.
Upvotes: 0
Views: 277
Reputation: 43876
You need to build an array of the two strings foreach element and then use SelectMany
to concatinate them:
string[] elementArray = elements.Select(
(a, i) => new string[] {
"list elment " + (i + 1) + " - " + a.SelectMe1,
"list elment " + (i + 1) + " - " + a.SelectMe2
}).SelectMany(x => x).ToArray();
or for a better looking c# 6 version:
string[] elementArray = elements.Select(
(a, i) => new string[] {
$"list elment {i+1} - {a.SelectMe1}",
$"list elment {i+1} - {a.SelectMe2}"
}).SelectMany(x => x).ToArray();
Upvotes: 2
Reputation: 3744
var i = 0;
var list = new List<string>();
elements.ForEach(p=>{
i++;
list.Add("list element " + i + " - " + p.SelectMe1);
list.Add(("list element " + i + " - " + p.SelectMe2);
});
Upvotes: 0