I'm trying to simulate a .csv file with a list of property names as a header row followed by the actual list of properties for a list of items.
Currently I have it working, but now there is a need to only include the properties' header and value where the item has a value for that property anywhere in the list.
This is what I have currently:
public static string LeadsToCSVString(List<Lead> leads)
{
StringBuilder builder = new StringBuilder();
PropertyInfo[] properties = typeof(Lead).GetProperties();
builder.AppendLine(string.Join(",", properties.Select(x => x.Name)));
foreach (Lead lead in leads)
{
builder.AppendLine(string.Join(",", properties.Select(x => x.GetValue(lead))));
}
return builder.ToString();
}
Where Lead
is:
public Lead
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsOk { get; set; }
}
The List<Lead>
isn't guaranteed to have a value for all property values and there can even be a case where there is no value for a property like below:
FirstName,LastName,IsOK
Joe,,false
Adam,,true
What needs to be done is to only have header values and row column values where the property has an actual value. If I take the above example list, the CSV string would look like:
FirstName,IsOk
Joe,false
Adam,true
How can I modify my code to do this?
Some examples of what should happen if any object in the list has a value for the property:
FirstName,LastName,IsOK
Joe,Dirt,
Adam,,true
FirstName,LastName,IsOK
,Dirt,
Adam,,true
Aucun commentaire:
Enregistrer un commentaire