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 3237166
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:42:25+00:00 2026-05-17T17:42:25+00:00

Assume you have a CSV file with the following simplified structure LINE1: ID,Description,Value LINE2:

  • 0

Assume you have a CSV file with the following simplified structure

 LINE1:  ID,Description,Value
 LINE2:  1,Product1,2
 LINE3:  ,,3
 LINE4:  ,,4
 LINE5:  2,Product2,2
 LINE6:  ,,3
 LINE7:  ,,5 

I am using FileHelpers to read the CSV and have hooked up one the interfaces that allows me me to access the current line, after it has been read. Refer to this SO question for more background.

The issues is that using that approach I will need to write many more if statements to check all the fields that need to be copied. (I have at least, 6 csv files at the moment with the same ‘blank’ format all having more than 20 fields that need to be copied ~ 120 if statements. urggh)

Now this is not a micro optimisation exercise since there will be more files that will have this ‘incomplete’ format.

How can I update the previous record in an elegant way such that I wont have to write if conditions and declarations for each field?

  • 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-05-17T17:42:26+00:00Added an answer on May 17, 2026 at 5:42 pm

    The current solution is to annotate the required fields using a custom attribute called CopyMe and then use reflection to copy.

    [DelimitedRecord(",") ]
    [IgnoreFirst(1)] 
    public class Product
    {
    
        [CopyMe()] public int ID { get; set; }
        [CopyMe()] public string Description { get; set; }
    
        [FieldConverter(ConverterKind.Decimal)]
        public decimal Val{ get; set; }
    }
    

    with the AfterRead method looking like so…

    public void AfterRead(AfterReadEventArgs<RawVolsDataPoints> e)
    {
        var record = (Product)e.Record;
    
        if (PreviousRecord == null)
        {
            PreviousRecord = new Product();
             PreviousRecord = record;
        }
    
        if (String.IsNullOrEmpty(record.ID)) // null value indicates a new row
        {
            var ttype = typeof(Product);
            var fields = ttype.GetFields();
    
            var fieldsToCopy = fields.Where(field =>
                field.GetCustomAttributes(typeof(CopyMeAttribute), true).Any());
    
            foreach (var item in fieldsToCopy)
            {
                var prevvalue = item.GetValue(PreviousRecord);
                item.SetValue(record, prevvalue);
             }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume you have a CSV file with the following structure LINE1: ID,Description,Value LINE2: 1,Product1,2
I have a script that generates a csv file using the following code: header('Content-type:
Assume I have the following ; def test(): while 1: a = b time.sleep(60)
Assume I have one threadpool and each thread is running following method: void runMe(HashMap
Assume I have a csv.DictReader object and I want to write it out as
I have imported a csv file containing spatial area information in varchar, then converted
I have the following code: int rc; rc = sqlite3_exec(sqlite3_database, .import mydata.csv mytable, callback,
I have written a generic CSV file reader that I'm trying to use to
Assume I have the following: <input type=text> <div id=listofstuff> <div class=anitem> <span class=item name>Dog</span>
I have a php script that is reading a remote CSV file, and adding

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.