【问题标题】:C# run function while it returns list of objectsC#在返回对象列表时运行函数
【发布时间】:2012-08-17 08:22:47
【问题描述】:

我的 C# 函数一次返回 100 个列表对象。我想填充一个列表,直到这个函数不返回任何列表。

我正在尝试做这样的事情:

int lastSelectedId = 0;
while(ReturnListOfCustomers(lastSelectedId).Count > 0)
{
    List<Customers> newCustomers = ReturnListOfCustomers(lastSelectedId);
    CustomerList.Append(newCustomers);
    lastSelectedId  = newCustomers.Last().rowid;
}

...但在这种情况下,我将不得不在每个循环中调用 ReturnListOfCustomers 函数两次, 我可以一次调用一个来让它变得更好吗?

谢谢。

【问题讨论】:

    标签: c# function while-loop


    【解决方案1】:

    只需检查您的列表是否增长:

     // Assuming CustomerList is a List<Customers> defined and initialized somewhere
     int prevCount = -1;
     int lastSelectedId = 0;
     while (prevCount < CustomerList.Count) {
      prevCount = CustomerList.Count;
      CustomerList.Append(ReturnListOfCustomers(lastSelectedId));
    
      // Need to check for null return condition
      if (CustomerList.Count > 0) {
        lastSelectedId = CustomerList.Last().rowId;
      }
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用do/while

      int lastSelectedId = 0;
      int count;
      do 
      {
          List<Customers> newCustomers = ReturnListOfCustomers(lastSelectedId);
          count = newCustomers.Count;
          if (count > 0)
          {
              CustomerList.Append(newCustomers);
              lastSelectedId  = newCustomers.Last().rowid;
          }
      } while (count > 0);
      

      【讨论】:

      • 此代码不起作用...当 newCustomers.Count == 0 时,您将在设置 lastSelectedId 时遇到异常,需要在其周围设置保护条件
      • 在最后一项上,这是将一个空列表附加到 CustomerList,然后,由于 newCustomers 为空(count=0),它会在 Last().rowid 上引发异常;
      • 哦,因为我好多年没用do while了,差点忘了这个关键字的存在:)非常感谢!
      【解决方案3】:

      简单的for 循环有什么问题?

      for ( List<Customer> buffer = ReturnListOfCustomers(highWaterID) ; buffer.Count > 0 ; buffer = ReturnListOfCustomers( buffer[buffer.Count-1].rowid ) )
      {
        CustomerList.Append(buffer);
      }
      

      【讨论】:

        【解决方案4】:

        为了删除每个循环的两个调用,您必须将结果存储在一个变量中。我也会像这样重构你的代码:

        int lastSelectedId = 0;
        List<Customers> newCustomers;
        
        while ((newCustomers = ReturnListOfCustomers(lastSelectedId)).Count > 0)
        {
            CustomerList.Append(newCustomers);
            lastSelectedId  = newCustomers.Last().rowid;
        }
        

        至少在我看来,我认为它看起来比 do-while 循环更好。另一方面,do-while 循环的可读性可能更快。

        【讨论】:

          【解决方案5】:

          您可以通过将newCustomers 列表移到循环外来减少点击次数,然后在完成追加后在每个循环中更新一次列表。

          int lastSelectedId = 0;
          List<Customers> newCustomers = ReturnListOfCustomers(lastSelectedId);
          while (newCustomer.Count > 0)
          {
              CustomerList.Append(newCustomers);
              lastSelectedId = newCustomers.Last().rowid;
              newCustomers = ReturnListOfCustomers(lastSelectedId);
          }
          

          【讨论】:

            【解决方案6】:

            将第一次调用的结果存储在一个变量中。

            int lastSelectedId = 0;
            var a = ReturnListOfCustomers(lastSelectedId);
            while(a.Count > 0)
            {
                List<Customers> newCustomers = a;
                CustomerList.Append(newCustomers);
                lastSelectedId  = newCustomers.Last().rowid;
                a = ReturnListOfCustomers(lastSelectedId);
            }
            

            【讨论】:

            • count 永远不会增加 djs.. thi 最适合作为 do{}while 循环。
            • Count 的值将与原始示例中的值相同。他描述它的方式,这将正常工作。对于循环的每次迭代,计数将应用于存储在同一变量中的新数组。
            • 使用 do/while,如果您正在执行追加以外的操作,您将检查两次计数以保持相同的功能。您检查它以查看是否应该执行循环体的其余部分,然后再次检查循环条件。我几乎总是更喜欢直接的 while 循环而不是 do/while。
            猜你喜欢
            • 2019-03-16
            • 2021-08-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-08-15
            • 1970-01-01
            • 1970-01-01
            • 2021-09-09
            相关资源
            最近更新 更多