Creating a class did it. thanks to Andre Oosthuizen. So I created an interface and a class ExportData. There are still some things I don't understand and why it has to be done the way it is.The error messages from VS and the docs from MS were not helpfull. I could not have solved this without help.This is the final class:
namespace Boeken.Models
{
public class ExportData
{
public string? titel { get; set; }
public string? auteur { get; set; }
public string? hugo { get; set; }
public string? nebula { get; set; }
public string? locus { get; set; }
public string? categorie { get; set; }
public string? taal { get; set; }
public string? soort { get; set; }
}
}
And this is the final function:
[HttpPost]
public FileResult ExportToCSV(string? listToExport)
{
List<ExportData> Data = new List<ExportData>();
string[] book = new string[] { };
string[] bookHeader = new string[] { };
switch (listToExport)
{
case "NTK":
Data = _context.Boek
.Include(b => b.Auteur)
.Include(b => b.Categorie)
.Where(b => b.CategorieID == 1 && b.ISBN == "0000000000" && (b.Hugo > 0 || b.Nebula > 0 || b.Locus > 0))
.Select(b => new ExportData
{
titel = b.Titel,
auteur = b.Auteur!.DisplayName,
hugo = b.Hugo.ToString(),
nebula = b.Nebula.ToString(),
locus = b.Locus.ToString(),
categorie = "",
taal = "",
soort = ""
})
.ToList<ExportData>();
bookHeader = new string[] { "Titel", "Auteur", "Hugo", "Nebula", "Locus" };
break;
case "NTG":
Data = _context.Boek
.Include(b => b.Auteur)
.Include(b => b.Categorie)
.Where(b => b.CategorieID == 1 && b.Gelezen == false)
.Select(b => new ExportData
{
titel = b.Titel,
auteur = b.Auteur!.DisplayName,
hugo = b.Hugo.ToString(),
nebula = b.Nebula.ToString(),
locus = b.Locus.ToString(),
categorie = "",
taal = "",
soort = ""
})
.ToList<ExportData>();
bookHeader = new string[] { "Titel", "Auteur", "Hugo", "Nebula", "Locus" };
break;
case "WIN":
Data = _context.Boek
.Include(b => b.Auteur)
.Include(b => b.Categorie)
.Where(b => b.CategorieID == 1 && (b.Hugo > 0 || b.Nebula > 0 || b.Locus > 0))
.Select(b => new ExportData
{
titel = b.Titel,
auteur = b.Auteur!.DisplayName,
hugo = b.Hugo.ToString(),
nebula = b.Nebula.ToString(),
locus = b.Locus.ToString(),
categorie = "",
taal = "",
soort = ""
})
.ToList<ExportData>();
bookHeader = new string[] { "Titel", "Auteur", "Hugo", "Nebula", "Locus" };
break;
case "AUT":
Data = _context.Auteur
.Select(b => new ExportData
{
titel = "",
auteur = b.DisplayName,
hugo = "",
nebula = "",
locus = "",
categorie ="",
taal = "",
soort =""
})
.ToList<ExportData>();
bookHeader = new string[] { ",Auteur" };
break;
case "CAT":
Data = _context.Categorie
.Select(b => new ExportData
{
titel = "",
auteur = "",
hugo = "",
nebula = "",
locus = "",
categorie = b.CategorieNaam,
taal = ",",
soort = ","
})
.ToList<ExportData>();
bookHeader = new string[] { "Categorie," };
break;
case "SRT":
Data = _context.Soort
.Select(b => new ExportData
{
titel = "",
auteur = "",
hugo = "",
nebula = "",
locus = "",
categorie = "",
taal = "",
soort = b.SoortNaam
})
.ToList<ExportData>();
bookHeader = new string[] { "Soort," };
break;
case "TAA":
Data = _context.Taal
.Select(b => new ExportData
{
titel = "",
auteur = "",
hugo = "",
nebula = "",
locus = "",
categorie = "",
taal = b.TaalVoluit,
soort = ""
})
.ToList<ExportData>();
bookHeader = new string[] { "Taal," };
break;
default:
return File(Encoding.UTF8.GetBytes("Invalid table name"), "text/csv", "Boeken.csv");
}
StringBuilder sb = new StringBuilder();
for (int j = 0; j < bookHeader.Length; j++)
{
sb.Append(bookHeader[j] + ',');
}
sb.Append("\r\n");
foreach (var item in Data)
{
if (listToExport == "NTK" || listToExport == "WIN" || listToExport == "NTG")
{
var dataItem = (dynamic)item;
book = new string[] { dataItem.titel!, dataItem.auteur!, dataItem.hugo, dataItem.nebula, dataItem.locus };
}
else if (listToExport == "AUT")
{
var dataItem = (dynamic)item;
book = new string[] { dataItem.titel!, dataItem.auteur!, dataItem.hugo, dataItem.nebula, dataItem.locus };
}
else if (listToExport == "CAT")
{
var dataItem = (dynamic)item;
book = new string[] { dataItem.categorie };
}
else if (listToExport == "SRT")
{
var dataItem = (dynamic)item;
book = new string[] { dataItem.soort };
}
else if (listToExport == "TAA")
{
var dataItem = (dynamic)item;
book = new string[] { dataItem.taal };
}
for (int j = 0; j < book.Length; j++)
{
sb.Append(book[j] + ',');
}
sb.Append("\r\n");
}
return File(Encoding.UTF8.GetBytes(sb.ToString()), "text/csv", "Boeken.csv");
}