1

Having a simple model:

public class SimpleClass
{
    public string prop1 { get; set; }
    public string prop2 { get; set;}
}

And a collection:

List<SimpleClass> myCollection = new List<SimpleClass>();

I want to do the following:

var headerCollection = new List<string>();

foreach(var item in myCollection)
{
    headerCollection.add(item.prop1);
    headerCollection.add("Total");
    headerCollection.add("Date");
    //...
    headerCollection.add(item.prop2);
}

But I need to use linq to objects.

I tried:

var modifiedHeaders = myCollection
    .Select(item => new[] { item.CON_DESCRIPCION, "Total", "Date", item.prop2 });

But it generates a collection of type: List<string[]>. How to solve this?

3 Answers 3

3

You just have to use SelectMany instead of Select:

var mh = myCollection.SelectMany(i =>
    new[] { i.CON_DESCRIPTION, "Total", "Date", i.prop2 });
Sign up to request clarification or add additional context in comments.

Comments

1

Use SelectMany after Select:

var modifiedHeaders = myCollection
       .Select(item => new[] { item.CON_DESCRIPCION, "Total", "Date", item.prop2 })
       .SelectMany(x => x);

Comments

0

Change Select to SelectMany:

var modifiedHeaders = myCollection
    .SelectMany(item => new[] { item.CON_DESCRIPCION, "Total", "Date", item.prop2 });

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.