0

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.

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

0
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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.