4

我想从两个列表中向表中添加一行。我在第二个 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

4 回答 4

4

您的 foreach 循环是:

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

它循环相同的东西(value),但说其中的项目是不同的。[1]

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

所以你会有:

foreach (LiabilityAssignment la in ci.Something )

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

[1] 我应该注意到语法本身并没有错。如果 value 中的项目同时属于这两种类型(例如,一种是另一种的子类型),它会正常工作。但这看起来不像这里的情况。

于 2012-08-22T13:34:54.813 回答
1

第二个循环应该是

foreach (LiabilityAssignment la in ci)
于 2012-08-22T13:35:53.033 回答
1

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

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

于 2012-08-22T13:36:37.160 回答
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);
    }
}
于 2012-08-22T14:04:15.003 回答