LINQ : Dynamic select

2018-12-31 14:50发布

Consider we have this class :

    public  class Data
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get; set; }
    public string Field4 { get; set; }
    public string Field5 { get; set; }

}

How do I dynamically select for specify columns ? something like this :

  var list = new List<Data>();

  var result= list.Select("Field1,Field2"); // How ?

Is this the only solution => Dynamic LINQ ?
Selected fields are not known at compile time. They would be specified at runtime

标签: c# linq
7条回答
闭嘴吧你
2楼-- · 2018-12-31 15:41
var result = from g in list.AsEnumerable()
                select new {F1 = g.Field1,F2  = g.Field2};
查看更多
登录 后发表回答