简体   繁体   中英

Handle Routing values in Lists

I'm handling a route mapper and I'm a bit off track. I got a large table of routes similar to the following:

sourceUrl -> destinationUrl

  • a -> b
  • y -> a
  • q -> b
  • d -> x
  • b -> d
  • r -> q
  • c -> b

If I select the last node (d -> x), I would like to display the correct route or the related routes for the selected node. The table would look as follows:

  • y -> a
  • a -> b
  • c -> b
  • r -> q
  • q -> b
  • b -> d

Right now, Ive got a recursive method that returns a list, but I can't get the correct order. What I have right now is:

public List<RedirectNode> FindByDestinationUrl(string destinationUrl)
{
List<RedirectNode> nodeList = new List<RedirectNode>();
List<RedirectNode> temporalList = new List<RedirectNode>();
List<RedirectNode> helperList = new List<RedirectNode>();
try
{
    nodeList = _redirectManagerRepository.FindByDestinationUrl(destinationUrl);
    helperList = nodeList;
    if (nodeList.Count != 0)
    {
        foreach (RedirectNode node in nodeList)
        {
            temporalList = FindByDestinationUrl(node.SourceUrl);
            if (temporalList != null)
            {
                helperList.AddRange(temporalList);
                helperList.Reverse();
            }
        }
    }
}
catch(Exception ex)
{
    nodeList = null;
    LoggingFactory.GetLogger().Log(CLASS + "FindByDestinationUrl. Error. " + ex.Message.ToString());
}
nodeList = helperList;
return nodeList;
}

I'm receiving the following route:

  • a -> b
  • c -> b
  • q -> b
  • y -> a
  • b -> d

If you do it a simple, full recursive way ( https://dotnetfiddle.net/H2LAsr ) that returns:

  • y -> a
  • a -> b
  • r -> q
  • q -> b
  • c -> b
  • b -> d
  • d -> x

Not this order is what you want?

    //key is target, values are the sources
    private static readonly ConcurrentDictionary<string, HashSet<string>> targetMap = new ConcurrentDictionary<string, HashSet<string>>();
    private static void Add(string source, string target)
    {
        var node = targetMap.GetOrAdd(target, new HashSet<string>());
        node.Add(source);
    }
    public static IEnumerable<KeyValuePair<string, string>> FindTarget(string destination, bool recursive = true)
    {
        HashSet<string> node;
        if (targetMap.TryGetValue(destination, out node))
        {
            foreach (var source in node)
            {
                if (recursive)
                {
                    foreach (var child_route in FindTarget(source))
                    {
                        yield return child_route;
                    }
                }
                yield return new KeyValuePair<string, string>(source, destination);
            }
        }
    }
    public static void Main()
    {
        Add("a", "b");
        Add("y", "a");
        Add("q", "b");
        Add("d", "x");
        Add("b", "d");
        Add("r", "q");
        Add("c", "b");
        foreach (var route in FindTarget("x"))
        {
            Console.WriteLine("{0} -> {1}", route.Key, route.Value);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM