Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Nested foreach help - Cannot apply indexing with [] to an expression...

$
0
0

I am creating an excel spreadsheet and have an inner foreach of a column list and I am trying to get the outer foreach data dynamically based on the column name.  Instead of having if statements based on the column name I want to get the current row's data based on the "col" value.  I have highlighted the line below which is throwing me an error:

DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<String> columns = new List<string>();
columns.Add("FirstName");
columns.Add("LastName");
columns.Add("Email");
foreach (String column in columns)
{
    DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
    cell.DataType = CellValues.String;
    cell.CellValue = new CellValue(column);
    headerRow.AppendChild(cell);
}
sheetData.Append(headerRow);

IEnumerable<Models.Data.Admin.User> theusers = Repository.Users().ToList();
foreach (var dsrow in theusers)
{
    DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
    foreach (string col in columns)
    {
        DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
        cell.DataType = CellValues.String;cell.CellValue = new CellValue(dsrow[col].ToString()); // dsrow[col] throws Cannot apply indexing with [] to an expression of type 'User'        newRow.AppendChild(cell);
    }
    sheetData.Append(newRow);
}



DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();                List<String> columns = new List<string>();                columns.Add("FirstName");                columns.Add("LastName");                columns.Add("Email");               foreach (String column in columns)                {                    DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();                    cell.DataType = CellValues.String;                    cell.CellValue = new CellValue(column);                    headerRow.AppendChild(cell);                }                sheetData.Append(headerRow);
                IEnumerable<Models.Data.Admin.User> theusers = Repository.Users().ToList();                foreach (var dsrow in theusers)                {                    //string test = dsrow.;
                    DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();                    foreach (string col in columns)                    {                        DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();                        cell.DataType = CellValues.String;                        cell.CellValue = new CellValue(dsrow[col].ToString());                        newRow.AppendChild(cell);                    }                   sheetData.Append(newRow);                }


Viewing all articles
Browse latest Browse all 9386

Trending Articles