簡體   English   中英

如何在 List 中獲取 nextNode 和 PrevNode<ienumerable></ienumerable>

[英]How to Get nextNode and PrevNode in List<IEnumerable>

這是我的代碼

    public static class WorkFlowSteps
    {
        public static List<SaaSWorkflowMatrix> GetMatrixData()
        {
            List<SaaSWorkflowMatrix> matrixList = new List<SaaSWorkflowMatrix>()
            {
                new SaaSWorkflowMatrix()
                {
                     WorkFlowId="1",
                     UserName ="SubmitterUser"
                },
                new SaaSWorkflowMatrix()
                {
                     WorkFlowId="2",
                     UserName ="PeerUser"
                },
                new SaaSWorkflowMatrix()
                {
                     WorkFlowId="3",
                     UserName ="ManagerUser"
                },
                new SaaSWorkflowMatrix()
                {
                     WorkFlowId="4",
                     UserName ="ProcurementUser"
                },
                new SaaSWorkflowMatrix()
                {
                     WorkFlowId="5",
                     UserName ="SupplierUser"
                },
                new SaaSWorkflowMatrix()
                {
                     WorkFlowId="6",
                     UserName ="DataPrivacyUser"
                },
                new SaaSWorkflowMatrix()
                {
                     WorkFlowId="7",
                     UserName ="SecurityUser"
                },
            };
            return matrixList;
        }

        
    }

    public class SaaSWorkflowMatrix
    {
        public string? WorkFlowId;
        public string? UserName;


    }

    public static class WorkFlowStepsExtensions
    {
        public static SaaSWorkflowMatrix GetNextNode(this IList<SaaSWorkflowMatrix> coordinates, string username)
        {

            return coordinates.FirstOrDefault(c=>c.UserName==username);
           
        }

        public static SaaSWorkflowMatrix GetPrevNode(this IList<SaaSWorkflowMatrix> coordinates, string username)
        {

            return coordinates.FirstOrDefault(c => c.UserName == username);

        }




    }

要求是傳遞 UserName 必須返回下一個用戶名和上一個用戶名,即,如果我通過 SubmitterUserName,下一個用戶是 PeerUser,如果我通過 PeerUser,下一個用戶應該是 ManagerUser,上一個用戶應該是 submitteruser。

現在我有擴展函數 GetNextNode 和 GetPrevNNode,返回相同的節點。 是否可以在 List<> 中獲取上一個節點和下一個節點?

你必須做這樣的事情:

public static class WorkFlowStepsExtensions
{
    public static SaaSWorkflowMatrix GetNextNode(this IList<SaaSWorkflowMatrix> coordinates, string username)
    {
        for (int i = 0; i < coordinates.Count; i++)
        {
            if (coordinates[i].UserName == username)
                return coordinates[i + 1];  // Need error checking
        }
        return null;
    }

    public static SaaSWorkflowMatrix GetPrevNode(this IList<SaaSWorkflowMatrix> coordinates, string username)
    {
        for (int i = 0; i < coordinates.Count; i++)
        {
            if (coordinates[i].UserName == username)
                return coordinates[i - 1];  // Need error checking
        }
        return null;
    }
}

不過,這確實效率低下。 使用鏈表或其他數據結構可能會更好。 或跟蹤當前索引,以便您輕松計算下一個和上一個索引。

暫無
暫無

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

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