Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marceladjanoh opened this issue Jun 1, 2023 · 4 comments
Open

Comments

@marceladjanoh
Copy link

marceladjanoh commented Jun 1, 2023

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 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

@jehugaleahsa
Copy link
Owner

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.

@marceladjanoh
Copy link
Author

marceladjanoh commented Jun 1, 2023

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

@jehugaleahsa
Copy link
Owner

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.

@marceladjanoh
Copy link
Author

marceladjanoh commented Jun 1, 2023

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); ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants