I’ve been working to create a virtual file system for SQL databases and represent this in a web interface. While creating the classes in .NET I realised that I needed to be able to traverse all the ancestors of a file up to the root level and to flatten these ancestors into a list which could then be ordered by node level.

My DirectoryObject class has methods for getting the immediate ancestor (GetAncestor) as well as all ancestors (GetAncestors).

public class DirectoryObject
{
	public string NodeId { get; set;}
	
	public Int16 NodeLevel { get; set; }
	
	public string ObjectId { get; set; }
	
	public string ObjectName { get; set; }
	
	public DirectoryObjectType ObjectType { get; set; }
	
	public int? Size { get; set; }
	
	public DateTime CreatedDateTime { get; set; }
	
	public DateTime ModifiedDateTime { get; set; }
	
	public string CreatedBy { get; set; }
	
	public string ModifiedBy { get; set; }
	
	public bool IsDeleted { get; set; }
	
	public DirectoryObject Ancestor { get; set; }
	
	public List<DirectoryObject> Descendants { get; set; }
	
	public void GetAncestor(string connectionString)
	{
		if (NodeLevel == 0)
		{
			return;
		}
		
		Ancestor = Common.GetAncestorDirectoryObject(ObjectId, connectionString);
	}
	
	public void GetAncestors(string connectionString)
	{
		if (NodeLevel == 0)
		{
			return;
		}
		
		Ancestor = Common.GetAncestorDirectoryObject(ObjectId, connectionString);
		Ancestor.GetAncestors(connectionString);
	}
	
	public void GetDescendants(string connectionString)
	{
		Descendants = Common.GetDescendantDirectoryObjects(ObjectId, connectionString);
	}
}

In order to flatten these ancestors out into a list I then use a LINQ extension based on code from here and use the list to generate Bootstrap navigation breadcrumbs.

<div class="row">
	<div class="col-lg-12">
		<ol class="breadcrumb">
			@{
				Int16 nodeLevel = Model.NodeLevel;
				List<DirectoryObject> ancestors = Model.SelfAndTraverse(x => x.Ancestor).ToList();
				
				foreach (DirectoryObject ancestor in ancestors.OrderBy(x => x.NodeLevel))
				{
					<li>
						<a asp-controller="Data" asp-action="Browse" asp-route-objectId="@ancestor.ObjectId">@ancestor.ObjectName</a>
					</li>
				}
			}
		</ol>
	</div>
</div>

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *