I am very new to programming and I can’t seem to find a way around this. Maybe there is also a better way.
I am trying to get 2 values out of a single row in my database. I have connected to it using EF.
My code is:
using (var myEntities = new dataEntities())
{
var myValues = (from values in myEntities.PointValues
where values.PointID == dataValue && values.DataTime >= fromDate && values.DataTime <= toDate
select new
{ values.DataTime,
values.DataValue
}).ToList();
I then write them to a file with this code:
using (StreamWriter sw = new StreamWriter(@"c:\Test.csv"))
{
for (var i = 0; i < myValues.Count; i++)
{
sw.WriteLine(myValues[i]);
}
}
The problem is the resulting CSV file is like this:
{ DataTime = 1/20/2010 2:15:00 AM, DataValue = 11.72 }
How do I get rid of the { DataTime =, DataValue and the closing }? (DataTime and DataValue are the field labels in the database).
Many thanks for any assistance.
Kind regards,
Julian
You just need to change how you write out the information:
Right now, you’re writing out the anonymous class directly, instead of creating the output formatted as needed.
Alternatively, you could build the string directly in your query and write the results easily using:
This would just build a
IEnumerable<string>instead of building the anonymous class.I would use this approach if you’re not using the query results for anything other than writing to the file. If you’re using the anonymous class in other code in between, the first is going to keep you from needing to change that code.