Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:08:07+00:00 2026-06-05T20:08:07+00:00

I am very new to programming and I can’t seem to find a way

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:08:08+00:00Added an answer on June 5, 2026 at 8:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to programming and I am trying to write a program that
I am very new to programming with jQuery and I am trying to retrieve
I am very new to MPI programming, I am trying to run a program
I am very new to programming, so I apologize if this question seems absurdly
I am very new to programming, so please forgive me if this question seems
I'm very new to Java programming language so this is probably dumb question but
I'm very new to iphone programming. I'm trying to write an application where I
I am very new to Bluetooth programming, and thus I apologize if this question
I'm very new to programming and I am trying to use paperclip to add
This is my first question and I am very new in programming field. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.