FlatFiles icon indicating copy to clipboard operation
FlatFiles copied to clipboard

How to map a List<Object> and loop through it while writing

Open marceladjanoh opened this issue 1 year ago • 4 comments

I have to generate a fixed-length text file. How can I map a list of object in my main model? Let's assume that I have the following models:

   public class Employee
    {
        public string LastName { get; set; } = string.Empty;
        public string FirstName { get; set; } = string.Empty;
        public string Title { get; set; } = string.Empty;
        public Address EmployeeAddress { get; set; }
        public List<Child>? Children { get; set;}
    }
    public class Child
    {
        public string? ChildName { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; } = "M";
    }
  1. How can I map the List<Child> Children using FixedLengthTypeMapper ?
  2. I have a list of employee to write in the text file and while looping through every employee, if an employee has children, I need to loop through all the children and write them before moving to the next employee. In this kind of scenario, what can be the efficient way to do the writing.

Thanks

marceladjanoh avatar Jun 01 '23 10:06 marceladjanoh

It depends. Is there a fixed, maximum number of children? If so, I would recommend a separate column for each possible child, leaving them blank if not applicable. You can specify "complex" columns, so you can have a column that is a fixed record itself.

jehugaleahsa avatar Jun 01 '23 11:06 jehugaleahsa

Thanks for your prompt answer. The list of child is coming from a database, so I don't know in advance the exact number. In fact, my generated text file should be formated like this:

Employee 1
  Child1 Name - Age - Sex
  Child2  Name - Age - Sex
  ...
  Childn Name - Age - Sex
Employee 2
  Child2 Name - Age - Sex
  Child3  Name - Age - Sex
  ...
  Childn Name - Age - Sex
...
Employee n

What do you recommend? Thanks

marceladjanoh avatar Jun 01 '23 11:06 marceladjanoh

It's hard to have a fixed width file when the number of columns is not known ahead of time. If they are literal children, you could probably get away with 25 being your fixed count, since only a few people in history have had that many kids, even with remarrying. 😆 Alternatively suggest multiple files.

jehugaleahsa avatar Jun 01 '23 11:06 jehugaleahsa

Thanks. For my scenario, multiple files is not allowed. I will see if that can be approved. Please, If I have to loop through the Employees list and write the employee one by one in order to respect a certain format in the generated file, where should the mapping be done? can I do it in the loop? Is it efficient to do something like :

foreach (var emp in EmployeeList)
{
    mapper.Property(p => p.FirstName, new Window(20) { FillCharacter = ' ' });
    mapper.Property(p => p.LastName, new Window(20) { FillCharacter = ' ' });
    // Apply custom formatting ...
}

and then after the loop, write all the records at once with await mapper.WriteAsync(streamWriter, EmployeeList, options).ConfigureAwait(false); ?

marceladjanoh avatar Jun 01 '23 12:06 marceladjanoh