score:1

So what you are looking to do is basically map a set of properties. There are libraries for this sort of thing, Automapper is a good one. However, you can also accomplish this yourself with a reflective constructor in an inheriting class. That would look like this:

Assumptions:

  • Your class with 50 columns is called Cols
  • You can inherit from Cols


public class CustomColumns : Cols
{
    public int CustomColA { 
        get{
            return this.ColA + 10;
        }
    }
    public int CustomColB { 
        get{
            return this.ColB + 30;
        }
    }
    public int CustomColC { 
        get{
            return this.ColC + 50;
        }
    }
    public CustomColumns(Cols cols)
    {
        string[] localNames = this.GetType().GetMembers().Where(m => m.MemberType == MemberTypes.Property).Select(m => m.Name).ToArray();
        string[] ctorNames = cols.GetType().GetMembers().Where(m => m.MemberType == MemberTypes.Property).Select(m => m.Name).ToArray();
        string[] names = localNames.Intersect(ctorNames).ToArray();
        foreach (string s in names)
        {
            PropertyInfo propSet = this.GetType().GetProperty(s);
            PropertyInfo propGet = typeof(Cols).GetProperty(s);
            propSet.SetValue(this, propGet.GetValue(cols, null));
        }
    }
}

Here is a demo of this working in dotnetfiddle: https://dotnetfiddle.net/AKPYQD

score:-3

The .Select() is creating a new object. Even if you just wrote:

var newList = list.Select(x => new 
{  x  });

You've get a nested property. You are just going to have to explicitly assign every column to a new property (Do you really need all 50 of them?)

score:-2

Use an anonymous type within the Select:

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

Related Articles