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

Total Post:128

Posted on    May-13-2013 3:20 AM

 C# C# 

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


Total Post:604

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