LINQ : DYNAMIC SELECT

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
C# 
C#
 1613  View(s)
Ratings:
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. AVADHESH PATEL

    Post:604

    Points:4228
    Re: LINQ : Dynamic select

    Hi Ashish!


    Try as following

    var result = from g in list.AsEnumerable() 

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!