How to select Dynamic column from List

c#, linq

Solution

Thanks for providing ideas to my question.Spending couple of hours in Google I found solution .

public void Test() {
    var data = new[] {
        new TestData { X = 1, Y = 2, Z = 3 }
    ,   new TestData { X = 2, Y = 4, Z = 6 }
    };
    var strColumns = "X,Z".Split(',');
    foreach (var item in data.Select(a => Projection(a, strColumns))) {
        Console.WriteLine("{0} {1}", item.X, item.Z);
    }
}
private static dynamic Projection(object a, IEnumerable<string> props) {
    if (a == null) {
        return null;
    }
    IDictionary<string,object> res = new ExpandoObject();
    var type = a.GetType();
    foreach (var pair in props.Select(n => new {
        Name = n
    ,   Property = type.GetProperty(n)})) {
        res[pair.Name] = pair.Property.GetValue(a, new object[0]);
    }
    return res;
}
class TestData {
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }
}

Problem

I want to select columns dynamically from `List` as following. So what could be the best way? ``` //a objects list List<DashBoard> dashboardlist = (List<DashBoard>)objList; string strColumns = "RecDate,ModifiedDate"; objList = (from obj in dashboardlist select new { strColumns }).ToList(); ``` ///////////// Ok,Just forget Object List say I have database table which have number of column ID,Name,Age,sex,etc ..Then I have columnList to display and the columnList is change according to condition . SO I have List people; and List columnTemplate; so now I want to select the column based on the template .

Original source