Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing to a csv after extracting data from a db, cannot modify variable with data

using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.IO;

namespace writingCSV
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var PatientEmails = await GetPatients();
            Type myType = PatientEmails.GetType();
            Console.WriteLine(myType);
        }

        public class PatientSurvey: IDisposable
        {
            public string Location { get; set; }
            public string Email { get; set; }

            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }

        static public async Task<IEnumerable<PatientSurvey>> GetPatients()
        {
            var connectionString = "SERVER INFORMATION";
            
            using (var cnn = new SqlConnection(connectionString))
            {
                cnn.Open();

                var patientSurveys = await cnn.QueryAsync<PatientSurvey>("STORED PROC NAME",
                                null,
                                commandTimeout: 120,
                                commandType: CommandType.StoredProcedure);

                return patientSurveys;
            }
        }
    }
}

I’m attempting to write data to a CSV file from a database. I’ve successfully connected to the db and extracted the data into a C# object, but I cannot figure out how to modify my data so I can actually write it into the file. The PatientEmails variable has the data within it, but it seems like it’s just an instance of the PatientSurvey class.

If I run my variable through a foreach loop, it prints out writingCSV.Program.PatientSurvey for each time it loops.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I dont see any problem …. GetPatients return an IEnumerable of PatientSurvey so indeed looping on the list just print out writingCSV.Program.PatientSurvey.
What would you expect ?
If your goal is to print patients email then you should write something like

foreach (var p in patientEmails) {
   Console.WriteLine(p.Email);
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading