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

[Feature Request] Read Related Rows into a single object with a list of related objects #2293

Open
melucas opened this issue Oct 8, 2024 · 0 comments
Labels

Comments

@melucas
Copy link

melucas commented Oct 8, 2024

Is your feature request related to a problem? Please describe.
I have use case where the CSV file will contains multiple rows which are related and need to parse them into a single Master object which contains a list of Detail objects.

Id,Name,ItemId,Description

1,red,1,item 1 text,
1,red,2,item 2 text,
1,red,3,item 3 text
2,green,1,item 1 text
3,blue,1,item 1 text

The first column, Id, value would be the unique identifier for grouping the rows into a Master object and the 3rd column, ItemId, would be the unique identifier for related Detail object.

Assumption would be the csv file would be already sorted by Id, ItemId.

class Master 
{
  public int Id { get; set; }
  public string Name { get; set; }
  public List<Detail> { get; set; }
}

class Detail
{
  [Name("ItemId")]
  public int Id { get; set; }
  public string Description { get; set; }
}

Describe the solution you'd like
Using the example from Reading Multiple Record Types , add the configuration/mapping to support reading related rows and populating the required master/detail rows.

List<Master> records = csv.GetRecords<Master>().ToList();

A few options for handling the Id column occurring in the csv file after it has already been added (e.g. csv file was not correctly sorted):

  1. Add the Detail record to the matching Master object
  2. Have option to ignore duplicates
  3. Thrown an exception

Duplicate ItemIds in the Detail record could be handled the same as the Master object

Describe alternatives you've considered
This could be done using the Reading by Hand example. But it would be likely more efficient and faster to have the CsvHelper library handle this.

Additional context
I could not find a solution for this in the other issues, on StackOverflow, or the documentation. Let me know if I missed that.

@melucas melucas added the feature label Oct 8, 2024
@melucas melucas changed the title Read Related Rows into a single object with a list of related objects [Feature Request] Read Related Rows into a single object with a list of related objects Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant