c# - Entity framework to CSV float values -
i'm trying write object csv, thing objects have float valure exemple (14,9) want change them (14.9) won't cause problem csv format
string csv = ""; using (var ctx = new nbaentities2()) { var studentlist = ctx.totalstat.sqlquery("select * totalstat idplayer<5") .tolist<totalstat>(); list<object> mycollection = new list<object>(); string type = ""; foreach (var item in studentlist) { type = item.gettype().tostring(); mycollection.add(item); } string iteem = mycollection.first().tostring(); ienumerable<propertyinfo> props = mycollection.first().gettype().getproperties(); //header list<string> test = new list<string>(); csv += string.join(",", props.select(prop => prop.name)) + "\r\n"; //rows foreach (var entityobject in mycollection) { csv += string.join(",", props.select( prop => (prop.getvalue(entityobject, null) ?? "?").tostring() )) + "\r\n"; } } file.writealltext("d:/test.csv", csv.tostring());
probably in case simplest way change current thread culture restore it.
thread.currentthread.currentculture = new cultureinfo("en-us");
you can specify format provider in
(prop.getvalue(entityobject, null) ?? "?").tostring()
in case need check if prop.getvalue(entityobject, null)
iformattable
(or if single or double) apply tostring
specifying invariantculture
.
Comments
Post a Comment