In a C# List how to select all columns + several custom columns, so that in each row all columns are flat (not nested)?

Viewed 280

I have a simple List that each row of it has 50 columns. I want return all 50 columns + 3 custom columns but i want make each row of the list like a flat (not nested) object.

Example:

var newList = list.Select(x => new 
    {  x,
       d.CustomColA = x.ColA+10,
       d.CustomColB = x.ColB+30,
       d.CustomColC = x.ColC+50
    });

Result: It works well but each result row is like an nested object:

var row = newList.FirstOrDefault();
row.x.ColA
row.x.ColB
row.x.ColC
.....
row.CustomColA 
row.CustomColB 
row.CustomColB 

Expected Result:

var row = newList.FirstOrDefault();
row.ColA
row.ColB
row.ColC
.....
row.CustomColA 
row.CustomColB 
row.CustomColB 

I used dynamic type and wrote the following code but it did not return expected result:

var newList = list.Select(x =>
{
   dynamic d = x;
   d.CustomColA = x.ColA+10;
   d.CustomColB = x.ColB+30;
   d.CustomColC = x.ColC+50;
   return d;
   //return x;
});

Result in watch panel: 'newList.FirstOrDefault()' threw an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'


Update:

Attention: I wrote in my question that i have 50 columns and wrote an example to show you i do not want name all the columns in the Select! (I know I can write name of all 53 column in a Select!) So that is not the correct answer.

Attention2: In the real project i have complicated custom columns but i wrote very simple example here to show what i want. Please write your flexible answers. Thank you.

3 Answers
Related