I have list of objects that i want to export to excel:
//My export to excel function:
public static void ExportToExcel(string[,] data, string excelFilePath = null)
{
// .....
}
My list contains many columns so i would like to select specific columns and add them into 2d array, also add the header on the top
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Address Adress { get; set; }
public Evaluation Evaluation { get; set; }
// ... many more
}
List:
> IEnumerable<Student> students
Fields to be selected:
- Id
- Name
- Address.Zipcode
I can perform this with for loop however i'm interested in linq, also i would like to hear your advice about performance since the list has + 200k records
Edit Sample
Columns => ID Name Zipcode
Rows => values
ID Name Zip
1 Mike 1101
2 Jan 2250
3 Peter 4456
students.Select(i => new { i.Id, i.Name, i.Address.Zipcode }).ToArray()and save it to whatever array you want. Actually I am unsure of why you need a 2d array instead of 1d?ExportToExcel()should expect anIEnumerable<T>instead of astring[,]. This way you can export item by item and line by line without ever having all the +200k records in memory at the same time.