Nested lists and foreach loops

I want to add one row to a table from two lists. I get an error in the second foreach loop. Cannot convert Liability.LiabilityCheckpointInstance to Liability.LiabilityAssignment.

foreach (LiabilityCheckpointInstance ci in value) { //foreach (LiabilityAssignment la in value) //{ var tr = new TableRow(); var tc = new TableCell { Text = ci.CheckGroup }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.IxCheck.ToString() }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.CheckPointInfo.ToString() }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.RejectedBy }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.Role }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.Mistakes.ToString() }; tr.Cells.Add(tc); //ChkpLiabilityDataTable.Rows.Add(tr); foreach (LiabilityAssignment la in value ) { //var tr = new TableRow(); tc = new TableCell { Text = la.LiabileOrganizationName }; tc = new TableCell { Text = la.LiabileOrganizationName }; tr.Cells.Add(tc); tc = new TableCell { Text = la.LiablePersonName }; tr.Cells.Add(tc); tc = new TableCell { Text = la.Comment }; tr.Cells.Add(tc); ChkpLiabilityDataTable.Rows.Add(tr); } } 
+4
source share
4 answers

This should do the trick for you.

 foreach (LiabilityCheckpointInstance ci in value) { var tr = new TableRow(); var tc = new TableCell { Text = ci.CheckGroup }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.IxCheck.ToString() }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.CheckPointInfo.ToString() }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.RejectedBy }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.Role }; tr.Cells.Add(tc); tc = new TableCell { Text = ci.Mistakes.ToString() }; tr.Cells.Add(tc); // YOU NEED TO BUILD THESE UP FRONT SO YOU CAN LOOP THROUGH THE // CHILDREN SAFELY BELOW tr.Cells.Add(new TableCell()); tr.Cells.Add(new TableCell()); tr.Cells.Add(new TableCell()); // I DON'T KNOW IF YOUR PROPERTY IS NAMED LiabilityAssignments OR NOT // SO REPLACE THAT WITH WHAT EVER IS NECESSARY - BUT IT SHOULD BE THE // LIST OF LiabilityAssignment ON THE LiabilityCheckpointInstance OBJECT foreach (LiabilityAssignment la in ci.LiabilityAssignments) { tr.Cells[6].Text = la.LiabileOrganizationName; tr.Cells[7].Text = la.LiablePersonName; tr.Cells[8].Text = la.Comment; ChkpLiabilityDataTable.Rows.Add(tr); } } 
+1
source

Your foreach loops:

 foreach (LiabilityCheckpointInstance ci in value) foreach (LiabilityAssignment la in value ) 

It iterates over the same ( value ), but says that the elements in it are different. [1]

I would suggest from the context that the second should focus on ci.something , and not just on the value.

So you will have:

 foreach (LiabilityAssignment la in ci.Something ) 

Of course, you will need to change Something to any of your list.

[1] It should be noted that the syntax itself is not incorrect. If the elements in the value have both types, then (for example, one is a subtype of the other), it will work fine. However, this does not seem to be true.

+4
source

The second cycle should be

 foreach (LiabilityAssignment la in ci) 
+1
source

you use the "value" as the source list for both foreach statements. The value apparently contains LiabilityCheckpointInstance objects, so it fails when you try to use it as if it contained LiabilityAssignment objects.

Did you mean sa: foreach (LiabilityAssignment la in qi.)?

+1
source

All Articles