C#运行函数,同时返回对象列表

Teo*_*ahi 0 c# function while-loop

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

我想做这样的事情:

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

...但是在这种情况下,我将不得不ReturnListOfCustomers每次循环调用两次函数,我可以通过一次调用它来使其更好吗?

谢谢.

Eri*_* J. 7

你可以使用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);
Run Code Online (Sandbox Code Playgroud)