pzozulka
asked on
LINQ: How to create multiple objects for each loop iteration
I am using LINQ to loop through a List<List<string>>. The format of this collection looks like this:
QuoteNum, QuoteCoverage1, QuoteCoverage2, QuoteCoverage3, ...
Example:
1, Auto, Auto, Health
2, Health, Auto, Health
...
I would like to loop through this list, and create a new List<ImportRecords>. I would like to have the following sample output from the above input:
1 Auto
1 Auto
1 Health
2 Health
2 Auto
2 Health
class ImportRecord
{
string QuoteNumber { get; set; }
string QuoteCoverage{ get; set; }
}
Here's what I created so far to kind of get the point across of what I'm trying to accomplish.
In other words, I need to transpose a single list of strings that contains 3 different Quote Coverages, into a list of three separate objects.
QuoteNum, QuoteCoverage1, QuoteCoverage2, QuoteCoverage3, ...
Example:
1, Auto, Auto, Health
2, Health, Auto, Health
...
I would like to loop through this list, and create a new List<ImportRecords>. I would like to have the following sample output from the above input:
1 Auto
1 Auto
1 Health
2 Health
2 Auto
2 Health
class ImportRecord
{
string QuoteNumber { get; set; }
string QuoteCoverage{ get; set; }
}
Here's what I created so far to kind of get the point across of what I'm trying to accomplish.
private importRecords(List<List<string>> allRows)
{
List<ImportRecord> newList = new List<ImportRecord>();
allRows.ToList().ForEach(line =>
{
ImportRecord record = new ImportRecord();
record.QuoteNumber = line[0];
record.QuoteCoverage = line[1];
newList.Add(record);
ImportRecord record = new ImportRecord();
record.QuoteNumber = line[0];
record.QuoteCoverage = line[2];
newList.Add(record);
ImportRecord record = new ImportRecord();
record.QuoteNumber = line[0];
record.QuoteCoverage = line[3];
newList.Add(record);
});
}
In other words, I need to transpose a single list of strings that contains 3 different Quote Coverages, into a list of three separate objects.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
string val = "1,2,3,5";
List<string> Mylist = val.Split(',').ToList<stri