Manoj Nayak
Manoj Nayak

Reputation: 2509

Nested lists and foreach loops

I want to add a single row to the table from the two lists. i am getting 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);
    }
}

Upvotes: 4

Views: 2604

Answers (4)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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);
    }
}

Upvotes: 1

Chris
Chris

Reputation: 27609

Your foreach loops are:

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

It is looping over the same thing (value) but saying the items in it are different. [1]

I would assume from the context that the second should be looping over ci.something rather than just value.

So you would have:

foreach (LiabilityAssignment la in ci.Something )

Of course you will need to change the Something to whatever your list is.

[1] I should note that the syntax itself is not incorrect. If the items in value is of both types then (eg one is a subtype of the other) it would work fine. This doesn't look like it is the case here though.

Upvotes: 4

Binary Worrier
Binary Worrier

Reputation: 51711

Second loop should be

foreach (LiabilityAssignment la in ci)

Upvotes: 1

Ray
Ray

Reputation: 21905

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

Did you means to sa: foreach (LiabilityAssignment la in ci.)?

Upvotes: 1

Related Questions