F
FcabralJ
Guest
Hello,
Currently I have a list of object "LIST<class>" which I need to replace illegal chars (for instance semicolumn ";") before generating the CSV. However the following code it's taking 3x times more than without it:
public static void ListToCsv<T, V>(List<V> query, string path)
{
var propertiesDto = typeof(T).GetProperties().Select(x => x.Name).ToList();
var propertiesIQueryable = typeof(V).GetProperties().Where(x => propertiesDto.Contains(x.Name));
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(string.Join(UniversalVariables.csvDelimiter.ToString(), propertiesDto.Select(p => p)));
foreach (var row in query)
{
var obj = Activator.CreateInstance<T>();
string line = "";
foreach (var propertyQuery in propertiesIQueryable)
{
var value = RemoveIllegalChar(propertyQuery.GetValue(row, null));
//without the above line is 3x faster
//var value = propertyQuery.GetValue(row, null);
line += value + UniversalVariables.csvDelimiter.ToString();
}
sw.WriteLine(line.Remove(line.Length - 1));
}
}
}
private static string RemoveIllegalChar(object value)
{
var newValue = value.ToString();
if (value.GetType() == Type.GetType("System.String"))
{
// Updating date - time to get a valid .csv file:
newValue += newValue.Replace(";", ",").Replace(@"\t|\n|\r", "");
};
return newValue;
}
Before this method I have a LINQ query which maybe I could use to replace the chars, but I was not able to do it as my List turns into a List<string> instead of a List<class>:
var lstRecords = (from cp in dbLinq_2.Table
select cp).ToList();
Continue reading...
Currently I have a list of object "LIST<class>" which I need to replace illegal chars (for instance semicolumn ";") before generating the CSV. However the following code it's taking 3x times more than without it:
public static void ListToCsv<T, V>(List<V> query, string path)
{
var propertiesDto = typeof(T).GetProperties().Select(x => x.Name).ToList();
var propertiesIQueryable = typeof(V).GetProperties().Where(x => propertiesDto.Contains(x.Name));
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(string.Join(UniversalVariables.csvDelimiter.ToString(), propertiesDto.Select(p => p)));
foreach (var row in query)
{
var obj = Activator.CreateInstance<T>();
string line = "";
foreach (var propertyQuery in propertiesIQueryable)
{
var value = RemoveIllegalChar(propertyQuery.GetValue(row, null));
//without the above line is 3x faster
//var value = propertyQuery.GetValue(row, null);
line += value + UniversalVariables.csvDelimiter.ToString();
}
sw.WriteLine(line.Remove(line.Length - 1));
}
}
}
private static string RemoveIllegalChar(object value)
{
var newValue = value.ToString();
if (value.GetType() == Type.GetType("System.String"))
{
// Updating date - time to get a valid .csv file:
newValue += newValue.Replace(";", ",").Replace(@"\t|\n|\r", "");
};
return newValue;
}
Before this method I have a LINQ query which maybe I could use to replace the chars, but I was not able to do it as my List turns into a List<string> instead of a List<class>:
var lstRecords = (from cp in dbLinq_2.Table
select cp).ToList();
Continue reading...