Ir am trying to render content that is loaded from a CSV with Scriban. Unfortunately, its not rendering correctly unless I explicitly call ToDictionary to create another dictionary and I would like to understand why, and proper ways to fix it.

var reader = new StreamReader(csvFile.ToString());
var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
var records = csv.GetRecords<dynamic>();
    
var templateStr = "|{{ReportID}}|";

foreach(ExpandoObject record in records){
    var template = Template.Parse(templateStr);
    var row = (IDictionary<string,object>)record;

    var reportID = row["ReportID"]; // Works as dictionary

    var expando = template.Render(record);
    var expandoCast = template.Render(row);
    var expandoToDictionary = template.Render(record.ToDictionary(x=>x.Key, x=>x.Value));

    Console.WriteLine($"{reportID} expando: {expando} expandoCast:{expandoCast} expandoToDictionary:{expandoToDictionary}");  
}

Prints

3940268 expando: || expandoCast:|| expandoToDictionary:|3940268|

Why does the template not render the keys? Is there a better way to solve this without creating a new dictionary?

0

There are 0 best solutions below