Link to home
Start Free TrialLog in
Avatar of pzozulka
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.
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);
	});
}

Open in new window


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.
Avatar of jitendra patil
jitendra patil
Flag of India image

you can try the below kind of example

string val = "1,2,3,5";
List<string> Mylist = val.Split(',').ToList<string>();
SOLUTION
Avatar of ROMA CHAUHAN
ROMA CHAUHAN
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial