Using LinqToExcel, I'm trying to create a dynamic list of objects that have a dynamic number of properties (with values). Essentially, the list of objects should directly represent the contents of an Excel sheet. However, I don't know the header names or the number columns before hand.
The code below does not work, it doesn't even compile, but I'm hoping it will show what I'm trying to do. Also it is missing a way to loop and add the correct number of properties. The names and number of properties should come from an array of column headers columnNameList
.
// get the records
var excel = new ExcelQueryFactory(path);
IEnumerable<string> columnNameList = excel.GetColumnNames(mod.SelectedSheet);
var ExpandoObject = (from x in excel.Worksheet(selectedSheet)
select new ExpandoObject()
{
ExpandoObject.Prop1 = x["excelCol1"],
ExpandoObject.Prop2 = x["excelCol2"],
ExpandoObject.Prop3 = x["excelCol3"],
ExpandoObject.Prop4 = x["excelCol4"],
ExpandoObject.Prop5 = DateTime.Now
}).ToList();