Can I get values from my database using this method but without generating the additional text?

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

Answers


You just need to change how you write out the information:

using (var sw = new StreamWriter(@"c:\Test.csv"))
{
    foreach(var value in myValues)
    {
        sw.WriteLine(string.Format("{0}, {1}", value.DataTime, value.DataValue));
    }
}

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:

 var myValues = (from values in myEntities.PointValues
              where values.PointID == dataValue && values.DataTime >= fromDate && values.DataTime <= toDate
              select new 
              { values.DataTime, 
                values.DataValue 
              })
             .AsEnumerable()
             .Select(v => string.Format("{0}, {1}", v.DataTime, v.DataValue));
 File.WriteAllLines(@"c:\Test.csv", myValues);

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.


Add another select at the end.

var myValues = (from values in myEntities.PointValues 
               where values.PointID == dataValue 
                     && values.DataTime >= fromDate 
                     && values.DataTime <= toDate 
               select new 
               { values.DataTime, 
                 values.DataValue 
               })
               .ToList()
               .Select(x => string.Format("{0}, {1}", x.DateTime, x.DataValue);

Additionally, if you don't need bells & whistles of the StreamWriter you could also use File.WriteAllLines():

File.WriteAllLines(@"c:\Test.csv", myValues);

(updated per Reed's post)


Need Your Help

DLookup Or DMax To Find Value

ms-access access-vba lookup

I am sure this is fairly simple put I am having trouble getting started on this. I use a Form to invoice clients which includes the field [Billing_Month]. What I'm looking to accomplish is this. ...