gpt4 book ai didi

c# - 嵌套列表和 foreach 循环

转载 作者:太空狗 更新时间:2023-10-30 00:43:10 24 4
gpt4 key购买 nike

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

最佳答案

您的 foreach 循环是:

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

它循环遍历同一事物 (value),但表示其中的项目不同。 [1]

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

所以你会:

foreach (LiabilityAssignment la in ci.Something )

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

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

关于c# - 嵌套列表和 foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12074331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com