lundi 22 février 2016

C# Sort list of class properties based on another List strings

I loop through the list of properties using reflection and assign the table cell that value. My class properties that are being looped through are assigned to the wrong column (header).

How do I sort the dataList properties names based on the header list, they are both named the same? I would rather do it this way than sort header list based on properties.

dataList type will always be a class with properties.

public void SetTableStrong<T>(List<T> dataList, List<string> header)
{       
    // Define our columns
    Column c = null;
    foreach (var item in header)
    {
        c = table.addTextColumn(item);
        c.horAlignment = Column.HorAlignment.CENTER;
    }

    // Initialize Your Table
    table.initialize();
    table.data.Clear();

    foreach (var item in dataList.Select((value, index) => new { value, index }))
    {
        Datum d = Datum.Body(item.index.ToString());

        //Property set to wrong header because they are not sorted to the same as headers.
        foreach (var property in item.value.GetType().GetProperties())
        {
            var value = property.GetValue(item.value, null);

            if (value == null)
                continue;

            d.elements.Add(value.ToString());
        }
        table.data.Add(d);
    }

    // Draw Your Table
    table.startRenderEngine();
}





Aucun commentaire:

Enregistrer un commentaire