I use Linq2SQL to connect and retrieve data from my databases.
I now need to write a csv file out and am using LinqToCsv. Nice utility as it checks for fields that need to be escaped or quoted when writing the file.
Code to do this is quite simple:
using (var _context = new WorkOrdersDataContext())
{
var workorders = _context.workorder.Where(i => i.reportdate > new DateTime(2022,
1, 1) && i.siteid == "PWRBLK" && i.woclass == "WORKORDER")
.Select(i => new {
i.wonum,
i.workorderid,
i.description,
i.status,
i.psgc_plannergroup,
i.location,
i.assetnum,
i.psgc_plantcond,
i.psgc_projectcode,
i.worktype,
i.targstartdate,
i.wopriority
}).ToList();
CsvFileDescription outputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true
};
CsvContext _csv = new CsvContext();
_csv.Write(workorders, "workorders.csv", outputFileDescription);
According to the documentation, formatting the output is done via a CvsColumn attribute in the class description. I believe I have this correct:
[CsvColumn(OutputFormat = "yyyy-mm-dd")]
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_targstartdate", DbType="DateTime", UpdateCheck=UpdateCheck.Never)]
public System.Nullable<System.DateTime> targstartdate
{
get
{
return this._targstartdate;
}
set
{
if ((this._targstartdate != value))
{
this._targstartdate = value;
}
}
}
This does not change the output however.
And secondly, this is in an auto generated file, so if I ever have to alter the .dbml file, it will get over written.
Suggestions?