LINQ : Dynamic select

Total Post:128


 2013  View(s)
Rate this:
Hi Expert!

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 ?

  Selected fields are not known at compile time. They would be specified at run time

Thanks in advance!
Any help will be appreciated!

  1. Re: LINQ : Dynamic select

    Hi Ashish!

    Try as following

    var result = from g in list.AsEnumerable() 

    select new {F1 = g.Field1,F2  = g.Field2};


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.