Question

I have this code. I receive several lists as parameters that I insert into a list of objects.

public static void SaveTransactionsToSeparateFiles(List<Receivables> genRecList, 
    List<Dilution> genDilList, List<Accountable> genAccList, string excelFile, 
    string typeReport, List<Outstanding> genOutList = null)
{
    List<object> listObj = new List<object> 
    { 
        genRecList, 
        genDilList, 
        genAccList, 
        genOutList 
    };
}

Then I have a foreach where I run through all the lists in List(object) listObj, turns them into datatables and adds the content of each lists in each datatable.

foreach (var obj in listObj)
{
    DataTable genTable = HelperMethods.ConvertListToDataTable(obj, true);
    Worksheet pantaReiWorkSheet = pantaReiWorkSheets[sheet.SheetName];

    int iRow = 0;
    foreach (DataRow row in genTable.Rows)
    {
        iRow++;

        int iCol = 0;
        foreach (DataColumn col in genTable.Columns)
        {
            iCol++;
            pantaReiWorkSheet.Cells[iRow + 1, iCol] = row[col.ColumnName];
        }
    }

    i++;
}

Then I have the convert to datatable method.

public static DataTable ConvertListToDataTable(object genList, bool toExcel)
{
    DataTable genDt = new DataTable();
    PropertyInfo[] p =  p.genList.GetType().GetProperties();

    for (int prop = 1; prop < p.Length - 2; prop++)
    {
        genDt.Columns.Add(new DataColumn(p[prop].Name, p[prop].PropertyType));
    }

    foreach (object t in (IEnumerable<object>)genList)
    {
        DataRow genDR = genDt.NewRow();

        for (int prop = 1; prop <  p.Length - 2; prop++)
        {
            genDR[p[prop].Name] = p[prop].GetValue(t, null);
        }

        genDt.Rows.Add(genDR);
    }

    return genDt;
}   

The thing is, I'm not geting the properties of each list inside the List listObj. So, how can I access the List<Receivables> genRecList, List<Dilution> genDilList, List<Accountable> genAccList, List<Outstanding> genOutList properties??

I had this done before, but with List(T) and one list at a time and it worked. Now it's a mess.

Was it helpful?

Solution

If you want to get Type of class T in List<T>:

var list = new List<T>();
var typeT = list.GetType().GenericTypeArguments.First();

So your code should be changed:

PropertyInfo[] p =  genList.GetType().GenericTypeArguments
                           .First().GetProperties();

OTHER TIPS

It seems to me that you make it harder on yourself than necessary. You start with lists of which you know the types. Then you throw them in a nondescript pile of lists, the type information gets buried and you give yourself a hard time digging it up again.

How to fix this?

Basically, you do two things in one: create DataTables and write them to Excel. It would help to separate these concerns. What about this:

  • create a method DataTable ConvertListToDataTable<T>(List<T>) and use the generic type parameter T to get PropertyInfo's
  • invoke this method for each list separately and put the data tables in a list.
  • then do something like SaveDataTablesToSeparateFiles(List<DataTable> tables, string excelFile, string typeReport)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top