mardi 29 novembre 2016

How to dynamically specify the properties that are to be selected from a Generic List?

Suppose I have a Class:

public class InstanceDataLog
    { 
        public long METERID { get; set; }
        public string Meter_Name { get; set; }     
        public string Date { get; set; }
        public Nullable<double> Vrn { get; set; }
        public Nullable<double> Vyn { get; set; }
        public Nullable<double> Vbn { get; set; }
   }
public void Export(string colnames)//Vrn,Vyn
        {
 List<InstanceDataLog> lst = new List<InstanceDataLog>();
            List<InstanceDataLog> lstrefined = new List<InstanceDataLog>();
            lst=   (List<InstanceDataLog>)TempData["InstanceDataList"];
    refinedlist=lst.Select(e => new {e.Vrn , e.Vyn }).ToList(); // I want to replace hardcoded e.Vrn , e.Vyn  with colnames
        }

I want to replace hardcoded e.Vrn , e.Vyn with colnames (which is a comma seperated list of property names

Assume that I can not change the parameter type of Export Method. It shall remain a comma seperated string of parameter names. Also the refined list should be a list not any object that I can not use as a datasource for GridView Class object.





Aucun commentaire:

Enregistrer un commentaire