0

I have some data which represents parent-child relationship on the same entity. Given a node, I need to find its entire upper hierarchy (parent, grand-parent, great grand-parent, etc..)

My entity is like this:

 public partial class Location{
        public int LocationId { get; set; }
        public int? FkParentLocationId { get; set; }
        ..... more properties here.......
        public virtual Location FkParentLocation { get; set; }
        public virtual ICollection<Location> InverseFkParentLocation { get; set; }
}

I'm referring to the Hierarchy traverse implementation suggested here, but it works when you go down the hierarchy. How would retrieve the upper hierarchy using LINQ?

Sample data:

            List<Location> locations = new List<Location> {
            new Location { LocationId = 5, FkParentLocationId = 3, LocationName = "Windsor", LocationDisplayName = "Windsor"},
            new Location { LocationId = 15, FkParentLocationId = 3, LocationName = "Hampshire", LocationDisplayName = "Hampshire" },
            new Location { LocationId = 12, FkParentLocationId = 3, LocationName = "Sussex", LocationDisplayName = "Sussex"},
            new Location { LocationId = 13, FkParentLocationId = 3, LocationName = "Willowood", LocationDisplayName = "Willowood"},
            new Location { LocationId = 1, FkParentLocationId = 3, LocationName = "Gerbshire", LocationDisplayName = "Gerbshire"},
            new Location { LocationId = 3, FkParentLocationId = 2, LocationName = "Lincoln", LocationDisplayName = "Lincoln"},
            new Location { LocationId = 2, LocationName = "Mains", LocationDisplayName = "Mains" }            };

Expected output: given location Id:5, I should get a list containing the locations 3 and 2 (as they are the parents).

devC
  • 1,384
  • 5
  • 32
  • 56

1 Answers1

0

Here is an approach you could use, demoed with a console app. Heavily borrowing from Jon Skeet.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Locations
{
    public partial class Location 
    {
        public int LocationId { get; set; }
        public int? FkParentLocationId { get; set; }
        public virtual Location FkParentLocation { get; set; }
        public virtual ICollection<Location> InverseFkParentLocation { get; set; }
        public string LocationName { get; set; }
        public string LocationDisplayName { get; set; }      
    }  

    class Program
    {
        static void Main(string[] args)
        {
            List<Location> locations = new List<Location> {
            new Location { LocationId = 5, FkParentLocationId = 3, LocationName = "Windsor", LocationDisplayName = "Windsor"},
            new Location { LocationId = 15, FkParentLocationId = 3, LocationName = "Hampshire", LocationDisplayName = "Hampshire" },
            new Location { LocationId = 12, FkParentLocationId = 3, LocationName = "Sussex", LocationDisplayName = "Sussex"},
            new Location { LocationId = 13, FkParentLocationId = 3, LocationName = "Willowood", LocationDisplayName = "Willowood"},
            new Location { LocationId = 1, FkParentLocationId = 3, LocationName = "Gerbshire", LocationDisplayName = "Gerbshire"},
            new Location { LocationId = 3, FkParentLocationId = 2, LocationName = "Lincoln", LocationDisplayName = "Lincoln"},
            new Location { LocationId = 2, LocationName = "Mains", LocationDisplayName = "Mains" }            };


            var result  = GetAncestorsIds(locations, 5);
            foreach (var id in result)
            {
                System.Console.WriteLine(id);
            }          
        }

        private static IEnumerable<int> GetAncestorsIds(List<Location> locations, int id)
        {
            Location location = locations.SingleOrDefault(l => l.LocationId == id);
            if(location != null)
            {   
                while(location != null && location.FkParentLocationId != null)
                {
                    location = locations.SingleOrDefault(l => l.LocationId == location.FkParentLocationId);
                    if(location != null)
                    {
                        yield return location.LocationId;
                    }
                }
            }   
        }
    }
}

And this approach can be turned into your own Linq extension. Here's how it could look.

public static class MyExtensions
{
    public static IEnumerable<int> GetAncestorIds<TSource>(this IEnumerable<TSource> source, Func<TSource, int> pk, Func<TSource, int?> fk, int id)
    {
        TSource currentObj = source.SingleOrDefault(s => pk(s) == id);

        while(currentObj != null && fk(currentObj) != null)
        {
            currentObj = source.SingleOrDefault(s => pk(s) == fk(currentObj));
            if(currentObj != null)
            {
                yield return pk(currentObj);
            }
        }
    }
}

and then to call this for your scenario you would do this

var result = locations.GetAncestorIds(l => l.LocationId, l => l.FkParentLocationId, 5);
Dave Barnett
  • 2,045
  • 2
  • 16
  • 32