2

在此处输入图像描述

我只是想循环所有在此处输入图像描述

results 对象中的 items[x] (返回我拥有的这个对象的函数,它基本上只返回一个字符串列表)。这来自我为 Web 服务目的创建的 vb.net 函数。我认为做一个 foreach 循环会是最简单最有效的事情,但我不能这样做,因为我的类没有实现 IEnumerable。但是我想知道是否还有更简单的方法可以在不使用 iEnumerable 的情况下循环它。您的帮助将不胜感激。这就是我的函数返回的类的样子。

Public Class GetErrors 
    Public FWWDIR_ERR_VENDOR As String = String.Empty
    Public FWWDIR_ERR_INVOICE As String = String.Empty
    Public FWWDIR_ERR_ACCOUNT As String = String.Empty
    Public FWWDIR_ERR_FUND As String = String.Empty
    Public FWWDIR_ERR_ORGN As String = String.Empty
    Public FWWDIR_ERR_PROG As String = String.Empty
    Public FWWDIR_ERR_ADDRSS_TYP As String = String.Empty
    Public FWWDIR_ERR_ADDRSS_SEQ As String = String.Empty
    Public FWWDIR_ERR_LOCATION As String = String.Empty
    Public FWWDIR_ERR_ACTIVITY As String = String.Empty
    Public FWWDIR_ROW As String = String.Empty
    Public service_error As String = String.Empty
    Public FWWDIR_ERR_PO As String = String.Empty
    Public FWWDIR_ERR_CHKVEND As String = String.Empty
    Public FWWDIR_ERR_FSYR As String = String.Empty
    Public FWWDIR_ERR_FSPD As String = String.Empty
    Public FWWDIR_ERR_ONTM_VEND As String = String.Empty
    Public FWWDIR_ERR_ONTM_ADRS1 As String = String.Empty
    Public FWWDIR_ERR_ONTM_CITY As String = String.Empty
    Public FWWDIR_ERR_ONTM_STATE As String = String.Empty
    Public FWWDIR_ERR_ONTM_ZIP As String = String.Empty
    Public FWWDIR_ERR_ENCUM_AMT As String = String.Empty
    Public FWWDIR_ERR_ENCUM_VEND As String = String.Empty
    Public FWWDIR_ERR_ITEM_ERROR As String = String.Empty
    Public FWWDIR_POHD As String = String.Empty
End Class

我试过的短代码片段。

ServiceApUtils _apUtils = new ServiceApUtils();
var   results = _apUtils.ADP_Processor(l_taxcode.ToArray(), l_amount.ToArray(), l_row.ToArray());
//Response.Write(results[0].)
if (results[0].service_error!="")
{
    Response.Write(results[0].service_error);

}
else if (results.Count > 0)//
{
    for (int x = 0; x < results.Count; x++ ) //typecast x a get error to loop the results
    {
        TableRow tr = new TableRow();
        Table1.Rows.Add(tr);
        foreach (string value in results[x])//fails here with Ienumerable error
        {
           TableCell tc = new TableCell();
           tc.Text = results[x].FWWDIR_ROW + results[x].FWWDIR_ERR_INVOICE + results[x].FWWDIR_ERR_ACCOUNT;
           tr.Cells.Add(tc);// = new TableCell();
        }
    }
}

无论我尝试 iEnumerable 错误还是我根本不知道如何在每个数组项内循环,我都会收到错误。

使用类作为返回的函数正在执行此操作

Public Function ADP_Processor(
    ByVal w_taxcode As String(), _
    ByVal w_Amount As String(), _
    ByVal w_row As String()) As List(Of GetErrors)
4

2 回答 2

2

尝试删除您的 foreach 并拥有:

for (int x = 0; x < results.Count; x++ ) //typecast x a get error to loop the results
{
  TableRow tr = new TableRow();
  Table1.Rows.Add(tr);

  GetErrors value = results[x];

  TableCell tc = new TableCell();
  tc.Text = results[x].FWWDIR_ROW + results[x].FWWDIR_ERR_INVOICE + results[x].FWWDIR_ERR_ACCOUNT;
  tr.Cells.Add(tc);// = new TableCell();
}

似乎您正在尝试使用像方法这样的类,因此像字符串数组一样使用字段。“结果”中的每个项目都是 GetErrors 类的一个实例,除非您实现 IEnumerable 并让 GetEnumerator 返回每个字段中的值,否则您不能在 foreach 中使用它。

于 2013-06-12T05:28:10.450 回答
2

我认为问题出在你的循环中:

 foreach (string value in results[x])//fails here with Ienumerable error
                       {

                           TableCell tc = new TableCell();
                           tc.Text = results[x].FWWDIR_ROW + results[x].FWWDIR_ERR_INVOICE    +                                         results[x].FWWDIR_ERR_ACCOUNT;
                           tr.Cells.Add(tc);// = new TableCell();
                       }

您正在循环结果集的数组,但是您不能对 result[x] 执行 foreach,因为 foreach 要求 results[x] 是可枚举的。基本上,如果你要写:

foreach (string value in results){ 
   //logic here 
}

然后 value 获取存储在结果集的一个实例中的所有值。现在该值是您的字符串数组,您可以使用简单的 for 循环遍历值,见下文:

      ServiceApUtils _apUtils = new ServiceApUtils();
           var   results = _apUtils.ADP_Processor(l_taxcode.ToArray(), l_amount.ToArray(),  l_row.ToArray());
                //Response.Write(results[0].)
             if (results[0].service_error!="")
             {
                 Response.Write(results[0].service_error);

             }//if the procedure has service errors
             else if (results.Count > 0)//
             {
                 foreach (var value in results){
                     TableRow tr = new TableRow();
                     Table1.Rows.Add(tr);
                       for (int i = 0; i < value.Length; i++{
                         //might have to update this logic as well:
                         TableCell tc = new TableCell();
                         tc.Text = value.FWWDIR_ROW + value.FWWDIR_ERR_INVOICE + value.FWWDIR_ERR_ACCOUNT;
                         tr.Cells.Add(tc);// = new TableCell();
                       }
      }
于 2013-06-12T05:29:06.730 回答