簡體   English   中英

C# 中的遞歸 function 中的問題

[英]Issue in recursive function in C#

我的遞歸 function 是

private dynamic FillRecursive(List<AllAccounts> flatObjects, Guid accountID)
{
    List<AllAccounts> recursiveObjects = new List<AllAccounts>();

    foreach (var item in flatObjects.Where(x => x.ParentID.Equals(accountID)))
    {
        recursiveObjects.Add(new AllAccounts
            {
                AccountID = item.AccountID ,                
                Children = FillRecursive(flatObjects, item.AccountID)
            });
    }

    return recursiveObjects;
}

當我從 postman 向此 function 發送請求時,它會停止 Visual Studio Code 中正在運行的程序。 請幫我解決這個問題。 謝謝

測試這是否有效

private dynamic FillRecursive(List<AllAccounts> flatObjects, Guid accountID,  List<AllAccounts> recursiveObjects=null)
{
    if(recursiveObjects==null)
       recursiveObjects = new List<AllAccounts>();

    foreach (var item in flatObjects.Where(x => x.ParentID.Equals(accountID)))
    {
        recursiveObjects.Add(new AllAccounts
            {
                AccountID = item.AccountID ,                
                Children = FillRecursive(flatObjects, item.AccountID,recursiveObjects)
            });
    }

    return recursiveObjects;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM