嵌套列表和foreach循环

时间:2012-08-22 13:32:02

标签: c# list

我想从两个列表中向表中添加一行。我在第二个foreach循环中遇到错误。 无法将Liability.LiabilityCheckpointInstance转换为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 个答案:

答案 0 :(得分:4)

你的foreach循环是:

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

它循环于同一个事物(value),但说它中的项目是不同的。 [1]

我会从上下文中假设第二个应该循环ci.something而不仅仅是值。

所以你会:

foreach (LiabilityAssignment la in ci.Something )

当然,您需要将Something更改为您的列表。

[1]我应该注意语法本身并不正确。如果值中的项目属于这两种类型(例如,一个是另一个的子类型),它将正常工作。这看起来并不像这样。

答案 1 :(得分:1)

第二个循环应该是

foreach (LiabilityAssignment la in ci)

答案 2 :(得分:1)

您使用'value'作为两个foreach语句的源列表。值显然包含LiabilityCheckpointInstance对象,因此当您尝试使用它时,它会失败,就像它包含LiabilityAssignment对象一样。

你的意思是sa:foreach(LiabilityAssignment la in ci。)?

答案 3 :(得分:1)

这应该适合你。

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