Home > Net >  Problems appending a class to a list in C#
Problems appending a class to a list in C#

Time:08-24

I have a list of class Contact that are read from a CSV file. I want to append a record to that and eventually write it out as a new file.

The CSV is read into a List of Contact called records.

If I add a breakpoint at the line that reads records.Append(cCust); and then step over it with watches on records and cCust, I can see that records contains the CSV data and that cCust contains the data to append, but as I step over the append, nothing happens; no errors and no additional record in the records list.

What am I doing wrong?

Here's the offending code:

   private void Btn_Merge_Click(object sender, EventArgs e)
    {
        IEnumerable<Contact> records;
        IEnumerable<dynamic> MondayRows;

        // Read the CSV into 'records'
        StreamReader Shopifyreader = new StreamReader(textBox_Shopify.Text);
        using (var ShopifyCSV = new CsvReader(Shopifyreader, CultureInfo.InvariantCulture))
        {
            records = ShopifyCSV.GetRecords<Contact>().ToList();
        }

        // Test adding a new record
        Contact cCust = new Contact();
        cCust.First_Name = "";
        cCust.Last_Name = "";
        cCust.Email = "[email protected]";
        cCust.Accepts_Email_Marketing = "";
        cCust.Company = "";
        cCust.Address1 = "";
        cCust.Address2 = "";
        cCust.City = "";
        cCust.Province = "";
        cCust.Province_Code = "";
        cCust.Country = "";
        cCust.Country_Code = "";
        cCust.Zip = "";
        cCust.Phone = "";
        cCust.Accepts_SMS_Marketing = "";
        cCust.Total_Spent = "";
        cCust.Total_Orders = "";
        cCust.Tags = "Send me Lead Marketing";
        cCust.Note = "";
        cCust.Tax_Exempt = "";

        records.Append(cCust);

        // We can now write out the modified file
        using (var writer = new StreamWriter(@"C:\temp\Output.csv"))
        using (var outputCSV = new CsvWriter(writer, CultureInfo.InvariantCulture))
        {
            outputCSV.WriteRecords(records);
        }

    }

    public class Contact
    {
        [Name("First Name")]  // This 'attribute' allows the class property First_Name to be matched to the CSV header "First Name"
        public string First_Name { get; set; }
        [Name("Last Name")]
        public string Last_Name { get; set; }
        public string Email { get; set; }
        [Name("Accepts Email Marketing")]
        public string Accepts_Email_Marketing { get; set; }
        public string Company { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        [Name("Province Code")]
        public string Province_Code { get; set; }
        public string Country { get; set; }
        [Name("Country Code")]
        public string Country_Code { get; set; }
        public string Zip { get; set; }
        public string Phone { get; set; }
        [Name("Accepts SMS Marketing")]
        public string Accepts_SMS_Marketing { get; set; }
        [Name("Total Spent")]
        public string Total_Spent { get; set; }
        [Name("Total Orders")]
        public string Total_Orders { get; set; }
        public string Tags { get; set; }
        public string Note { get; set; }
        [Name("Tax Exempt")]
        public string Tax_Exempt { get; set; }
     }

}

CodePudding user response:

This is the problem:

records.Append(cCust);

That's a LINQ extension method, which returns a sequence containing the original sequence and the new value afterwards. It doesn't modify the existing sequence.

So you could use:

records = records.Append(cCust);

However, I'd suggest changing the declared type of records to List<Contact> and then calling the Add method to add directly into the list:

records.Add(cCust);
  • Related