Arbaaz Arbaaz - 4 days ago 6
ASP.NET (C#) Question

How to extract particular properties from generic list based on property name sent as string array?

I have a Generic List of type

List<InstanceDataLog>
which has huge number of properties in it. I want to pass names of a few properties to a method and want to extract a refined List from within this list.

public void Export(string col) //a,b,c
{
string [] selectedcol = col.Split(',');
var grid = new GridView();
var data = TempData["InstanceDataList"];
List<InstanceDataLog> lst = new List<InstanceDataLog>();
List<EToolsViewer.APIModels.InstanceDataLog> lstrefined = new List<InstanceDataLog>();
lst= (List<EToolsViewer.APIModels.InstanceDataLog>)TempData["InstanceDataList"];

var r= lst.Select(e => new {e.a, e.b}).ToList();// I would like to replace these hardcoded properties with names of properties present in `selectedcol `

grid.DataSource =r;
grid.DataBind();
}


To clear things up further, suppose
InstanceDataLog
has 5 properties : a,b,c,d,e I would like pass a,b and be able to extract a new list with only properties a,b

EDIT:

$('#export').mousedown(function () {

window.location = '@Url.Action("Export", "TrendsData",new { col = "a,b,c" })';

});

Answer

You could use such method to get properties:

private object getProperty(EToolsViewer.APIModels.InstanceDataLog e, string propName)
    {
 var propInfo =typeof(EToolsViewer.APIModels.InstanceDataLog).GetProperty(propName);
    return propInfo.GetValue(e);        
}

and with another function you could get all properties you want:

private dynamic getProperties(string[] props, EToolsViewer.APIModels.InstanceDataLog e )
{
    var ret = new ExpandoObject() as IDictionary<string, Object>;;
    foreach (var p in props)
    {
        ret.Add(p, getProperty(e, p));
    }       
    return ret;
}

The problem occurs if you try to assign DataSource with expando object. Solution is described hier:

Binding a GridView to a Dynamic or ExpandoObject object

We do need one more method:

public DataTable ToDataTable(IEnumerable<dynamic> items)
{
    var data = items.ToArray();
    if (data.Count() == 0) return null;

    var dt = new DataTable();
    foreach (var key in ((IDictionary<string, object>)data[0]).Keys)
    {
        dt.Columns.Add(key);
    }
    foreach (var d in data)
    {
        dt.Rows.Add(((IDictionary<string, object>)d).Values.ToArray());
    }
    return dt;
}

and use it:

var r = lst.Select(e => getProperties(selectedcol, e)).ToList();
grid.DataSource = ToDataTable(r);

The same thing, but ready to run for LinqPad:

void Main()
{
    var samples = new[] { new Sample { A = "A", B = "B", C = "C" }, new Sample { A = "A1", B = "B2", C = "C1" } };
    var r = samples.Select(e => getProperties(new[] {"A", "C", "B"}, e)).ToList();
    r.Dump();
}

private object getProperty(Sample e, string propName)
{
    var propInfo = typeof(Sample).GetProperty(propName);
    return propInfo.GetValue(e);
}

private dynamic getProperties(string[] props, Sample e)
{
    var ret = new ExpandoObject() as IDictionary<string, Object>; ;
    foreach (var p in props)
    {
        ret.Add(p, getProperty(e, p));
    }
    return ret;
}

public class Sample
{
    public string A { get; set;}
    public string B { get; set;}
    public string C { get; set;}
}

With output:

enter image description here

Comments