You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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):
Add the Detail record to the matching Master object
Have option to ignore duplicates
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.
The text was updated successfully, but these errors were encountered:
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
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
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.
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):
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.
The text was updated successfully, but these errors were encountered: