Home > DeveloperSection > Forums > LINQ : Dynamic select
Ashish Pandey

Total Post:128

Points:898
Posted on    May-13-2013 3:20 AM

 C# C# 
Ratings:


 1 Reply(s)
 1221  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!



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-13-2013 9:34 AM

Hi Ashish!

Try as following

var result = from g in list.AsEnumerable() 

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


Don't want to miss updates? Please click the below button!

Follow MindStick