Ignore columns from a dynamic records list #1797
Unanswered
klaudyuxxx
asked this question in
Q&A
Replies: 1 comment 2 replies
-
You don't know the columns, but you do know ones you don't want? void Main()
{
var records = new List<dynamic>();
{
dynamic record = new ExpandoObject();
record.Id = 1;
record.Name = "one";
record.DateTime = DateTimeOffset.Now;
records.Add(record);
}
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, config))
{
for (var i = 0; i < records.Count; i++)
{
dynamic record = RemoveColumns(records[i]);
if (i == 0)
{
csv.WriteDynamicHeader(records[0]);
csv.NextRecord();
}
csv.WriteRecord(record);
csv.NextRecord();
}
csv.Flush();
writer.Dump();
}
}
public dynamic RemoveColumns(dynamic record)
{
var dictionary = (IDictionary<string, object>)record;
// Remove any columns here you don't want.
dictionary.Remove("DateTime");
return dictionary;
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using Dapper to Query an SQL procedure to which I don't know the returned columns.
I'm trying to configure CSVHelper to ignore some of the columns returned by the SQL Procedure, before writing them to a file.
Can you please point me to the right direction?
Beta Was this translation helpful? Give feedback.
All reactions