I have the following class which recurs on itself to form a tree-like data structure:
public class chartObject
{
public string name { get; set; }
public int descendants { get; set; }
public List<chartObject> children { get; set; }
}
For each object in the tree I would like to populate the descendant property with the amount objects that exist underneath it.
Example structure:
chartObject1 (descendants: 4)
└-chartObject2 (descendants: 0)
└-chartObject3 (descendants: 2)
└--chartObject4 (descendants: 1)
└---chartObject5 (descendants: 0)
What would be the most efficient way of doing this?
How about the recursive formula:
children.Count + children.Sum(c => c.descendants)
This is suitable for eager-evaluation / caching if the tree is immutable (which it isn't from the class declaration). If you want efficiency even in the face of mutability, you'll find this a lot more difficult; you can consider marking parts of the tree "dirty" as it is mutated / eagerly force the re-evalutation of this metric to "bubble up" as part of a tree is mutated.
This works for me:
public void SetDescendants(chartObject current)
{
foreach (var child in current.children)
{
SetDescendants(child);
}
current.descendants = current.children.Sum(x => 1 + x.descendants);
}
I tested with this code:
var co = new chartObject()
{
name = "chartObject1",
children = new List<chartObject>()
{
new chartObject()
{
name = "chartObject2",
children = new List<chartObject>() { }
},
new chartObject()
{
name = "chartObject3",
children = new List<chartObject>()
{
new chartObject()
{
name = "chartObject4",
children = new List<chartObject>()
{
new chartObject()
{
name = "chartObject5",
children = new List<chartObject>() { }
}
}
}
}
}
}
};
And got this as the result:
For calculations to be most efficient, cache their result in the node itself. Otherwise, you'll be re-calculating the count every time the descendants property is looked up.
The cost of doing that is the need to invalidate the cache all the way up the parent chain, like this:
public class chartObject
{
private chartObject _parent;
private int? _descCache = null;
public string name { get; set; }
public int descendants {
get {
return _descCache ?? calcDescendents();
}
}
public List<chartObject> children { get; set; }
public void AddChild(chartObject child) {
child._parent = this;
children.Add(child);
chartObject tmp = this;
while (tmp != null) {
tmp._descCache = null;
tmp = tmp._parent;
}
}
private int calcDescendents() {
return children.Count+children.Sum(child => child.descendants);
}
}
Walk all nodes of the tree (depth first is ok) and when done with children set "descendants property to sum of children's descendants + child count. You have to do it on every change to the tree structure. You should be able to limit updates only to parents of element that is changed.
If nodes made immutable you can populate the field at creation time.
Side notes:
Your tree is mutable as it is now (one can easily add more child nodes anywhere), so it may be safer to have method that counts descendants instead of property on a node.
Having computed property int descendants { get; set; } to be read/write is confusing as anyone can set its value to whatever number. Consider if making it read only and updating when one of child nodes changes (requires some custom notification mechanism).
Code style - consider naming classes with upper case names for code that is intended to be public (follow Microsoft's C# coding guidelines). chartObject -> ChartObject
Related
I have documents that are based on others, through recursion, I find documents that refer to what they are based on, the problem is that they are all duplicated into one, it does not work correctly
not the correct variant
I want to get this
In the code, the main thing that I transfer is the Type of the document and the List of child elements
public async Task<List<DocumentTreeItem>> FillRecursionTree(Guid documentId, string documentPresentationName, List<DocumentTreeItem> treeItems, Type documentType)
{
await FillRecursionTree(documentId, documentPresentationName, treeItems, documentType);
return treeItems;
async Task FillRecursionTree(Guid childrenId, string childrenIdDocumentPresentationName, List<DocumentTreeItem> childrenIdTreeItems, Type childrenIdDocumentType)
{
var currentNode = new DocumentTreeItem(documentPresentationName, childrenIdDocumentType, childrenIdTreeItems)
{
Id = childrenId,
DocumentPresentationName = childrenIdDocumentPresentationName,
TypeDocument = childrenIdDocumentType
};
childrenIdTreeItems.Add(currentNode);
var customerInvoices = await GetBaseCustomerInvoiceDocuments(childrenId);
foreach (var customerInvoice in customerInvoices)
{
childrenIdDocumentType = customerInvoice.GetType();
await FillRecursionTree(customerInvoice.Id, customerInvoice.DocumentPresentationName, **currentNode.Childrens,** childrenIdDocumentType);
}
}
}
if i think correctly i need to add elements to the root element but how do i do it so i don't get then all the elements again if they don't fit
currentNode.Childrens <-
How can I pass from the main document only the required array of child elements, and not all at once, where everything falls.
Maybe I'm wrong and the problem is in the second one.
This is the type in which I keep everything, there is a children's letter in which all the documents made on its basis are recorded.
public Guid Id { get; set; }
public string? DocumentPresentationName { get; set; }
public Type TypeDocument { get; set; }
public List<DocumentTreeItem> Childrens { get; set; } = new List<DocumentTreeItem>();
public DocumentTreeItem(string documentPresentationName, Type typeDocument, List<DocumentTreeItem> children = null)
{
DocumentPresentationName = documentPresentationName;
TypeDocument = typeDocument;
if (children != null)
Children.AddRange(children);
}
public List<DocumentTreeItem> Children
{
get
{
return Childrens;
}
}
I passed the entire array of elements of the root, and did not create a new one
var currentNode = new DocumentTreeItem(documentPresentationName, childrenIdDocumentType, **new List<DocumentTreeItem>()**)
{
Id = childrenId,
DocumentPresentationName = childrenIdDocumentPresentationName,
TypeDocument = childrenIdDocumentType
};
I am trying to add tree branch to IDictionary<int,Division> where int is depth of object and object shown below.
Example of dictionary result:
0, Parent
1, Parent.Child
2, Parent.Child.Child
class Division
{
public int Id { get; set; }
public int ParentId { get; set; }
public Division Parent { get; set; }
public ICollection<Division> Children { get; set; }
}
I am using recursion to iterate from root parent to child I need and at the same moment adding depth and Division to dictionary.
For iteration I am using this method.
public void GetBranchFromTop(Division division, int selectedNodeId, int selectedNodeDepth, ref IDictionary<int, Division> branch)
{
branch.Add(selectedNodeDepth, division);
if (division.Id == selectedNodeId)
{
return;
}
if (division.Children != null)
{
foreach (var child in division.Children)
{
selectedNodeDepth = selectedNodeDepth + 1;
GetBranchFromTop(child, selectedNodeId, selectedNodeDepth, ref branch);
}
}
}
This method works fine when object have only one child. When there is more children adding to dictionary is impossible because of depth key is repeating.
I figured that I need to add objects to dictionary only after finding object I am searching but nothing comes to mind how can I do it recursively.
I can think of 2 ways of doing it. First, you'll need have a separate branch dictionary for each tree branch - meaning for each call of GetBranchFromTop, like this
public void GetBranchFromTop(
Division division,
int selectedNodeId,
int selectedNodeDepth,
ref IDictionary<int, Division> foundBranch,
IDictionary<int, Division> currentBranch)
{
if(foundBranch != null){
return; //no need to continue search if the target branch was found already
}
currentBranch.Add(selectedNodeDepth, division);
if (division.Id == selectedNodeId)
{
foundBranch = currentBranch;
return;
}
if (division.Children != null)
{
var nextNodeDepth = selectedNodeDepth + 1;//note this is moved out of the loop
foreach (var child in division.Children)
{
var newBranch = new Dictionary<int, Division>(currentBranch); //copy the branch for each child.
GetBranchFromTop(child, selectedNodeId, nextNodeDepth, ref foundBranch, newBranch);
}
}
}
Note that you had an error in selectedNodeDepth = selectedNodeDepth + 1; - it was inside the loop, meaning it will be incremented for all the children, who are at the same +1 depth.
The second way where you don't need to drag all the branches around: you need to find Division you are looking for in the tree, say its targetDivision. When you've got it, build its tree branch using Parent links:
List<Division> parentsAndSelf = new List<Division>();
Division currentDivision = targetDivision;
while(currentDivision != null){
parentsAndSelf.Add(currentDivision);
currentDivision = currentDivision.Parent;
}
parentsAndSelf = parentsAndSelf.Reverse();
Now parentsAndSelf list will contain the desired Parent -> Child -> Child.Child list with target Division at the end. The depth will be the index of an item in the list.
(Title and question have been significantly changed, as the none of the important parts ended up being relevant to the problem)
I have a generated file tree of a hard drive, and I'm creating a function to highlight every instance of an extension in the file tree. For some reason iterating over any duplicate file tree other than the one created during the scan can take at least twice as long. Please note that I am not trying to iterate over the file tree during the scan.
What exactly is causing the slowdown? List<FileNode> seems to be the culprit, but I'm not sure what internal mechanism is at fault.
I've created a gist with 4 files to enumerate a file tree and show the inconsistencies in iteration times: FileTreeIterationSpeedTest
Performance for drive with 2m files and 200k directories:
Output from gist:
Scanning...
RAM Used: 300.6 MB
Bytes: 443.7 GB
Files: 1,925,131
Folders: 156,311
Progress: 100.0%
Duration: 00:00:17
Scan complete!
Duplicating file tree...
Duplication complete!
RAM Used: 311.4 MB
Iterating: 1000
Scanned Tree: 00:03.857
Duped Tree: 00:01.409
Duped Tree is 173.6% faster
Press any key to continue...
Relevant Code from FileNode.cs:
public class FileNode {
public enum FileNodeType {
Root,
Directory,
FileCollection,
File,
}
private readonly List<FileNode> children = new List<FileNode>();
private FileNode fileCollection;
public FileNode Parent { get; private set; }
public FileNodeType Type { get; }
public long Size { get; private set; }
public string Extension { get; } = string.Empty;
public string Name { get; }
// File Collection
private FileNode() {
Type = FileNodeType.FileCollection;
Name = "<Files>";
}
// Root Node
public FileNode(string drivePath) {
Type = FileNodeType.Root;
Name = drivePath;
}
// File or Directory Node
public FileNode(Win32FindData find) {
if (!find.IsDirectory) {
Type = FileNodeType.File;
Extension = Path.GetExtension(find.cFileName);
}
else {
Type = FileNodeType.Directory;
}
Name = find.cFileName;
Size = find.Size;
}
// Duplicate Tree \w Parent
public FileNode(FileNode root) : this(root, null) {
}
// Duplicate Tree \w Parent
private FileNode(FileNode file, FileNode parent) {
Parent = parent;
Type = file.Type;
Size = file.Size;
Extension = file.Extension;
Name = file.Name;
int count = file.children.Count;
children = new List<FileNode>(count);
for (int i = 0; i < count; i++)
children.Add(new FileNode(file[i], this));
}
public void AddChild(FileNode item) {
if (item.Type == FileNodeType.File && Type != FileNodeType.FileCollection) {
if (fileCollection == null)
fileCollection = new FileNode();
fileCollection.AddChild(item);
}
else {
children.Add(item);
item.Parent = this;
}
}
public bool IsLeaf => children.Count == 0;
public int Count => children.Count;
public FileNode this[int index] => children[index];
}
I have a SQL table like this:
DepartmentID is parent of department. I've build a tree by this table(in ASP.net (C#) project):
Records in tree above is:
I need to get parents in this tree.
I can do it in SQL Server like this(for Example id=2, id is input argument):
with cte1
as
(
select id,name,DepartmentID, 0 AS level
from Department
where id =2
union all
select Department.ID,Department.name,Department.DepartmentID, level+1
from Department
inner join cte1 on Department.ID=cte1.DepartmentID
)
select * from cte1
Output(id=2 (A))
Output(id=4 (A1))
I know EF does not support cte, but I need to get this result in EF.
It would be very helpful if someone could explain solution for this problem.
These posts are similar to your question.please see these:
writing-recursive-cte-using-entity-framework-fluent-syntax-or-inline-syntax
converting-sql-statement-that-contains-with-cte-to-linq
I think there is no way to write a single LINQ to SQL query that could get all However, LINQ supports a method to execute a query (strangly enough called DataContext.ExecuteQuery). Looks like you can use that to call a arbitrary piece of SQL and map it back to LINQ.
See this post:
common-table-expression-in-entityframework
The easiest way I can think of is to map the relationship in EF and then retrieve all departments and then get the root parent from that list. All of them should be loaded in memory and EF will take care of the tree structure with the mapping. Alternatively you can enable lazy loading and just get the parent but then with each child or childset a query will be executed by EF during retrieval.
Model
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public int? DepartmentId { get; set; }
public Department ParentDepartment { get; set; }
public virtual ICollection<Department> ChildDepartments { get; set; }
}
Mapping (using fluent)
public DbSet<Department> Departments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// other mapping code
modelBuilder.Entity<Department>()
.HasOptional(x => x.ParentDepartment)
.WithMany(x => x.ChildDepartments)
.HasForeignKey(x => x.DepartmentId);
// other mapping code
}
Eager retrieval of root parent
using (var context = new YourDbContext())
{
var allDepartments = context.Departments.ToList(); // eagerly return everything
var rootDepartment = allDepartments.Single(x => x.DepartmentId == null);
}
Retrieval of only root parent and then use lazy loading, note that the DbContext needs to be available for Lazy Loading to work and it must also be enabled on the DbContext
using (var context = new YourDbContext())
{
var rootDepartment = context.Departments.Single(x => x.DepartmentId == null);
// do other stuff, as soon as context is disposed you cant lazy load anymore
}
Try one of these,
1-
int _ID = 2; // ID criteria
List<object> result = new List<object>(); // we will use this to split parent at child, it is object type because we need Level
var departments = entites.Departments.Where(x => x.ID == _ID).SelectMany(t => entites.Departments.Where(f => f.ID == t.DepartmentID),
(child, parent) => new { departmentID = child.DepartmentID, Name = child.Name, ID = child.ID, level = 0,
Parent = new { DepartmentID = parent.DepartmentID, Name = parent.Name, ID = parent.ID, level = 1 }});
// first we check our ID (we take A from where criteria), then with selectmany T represents the Department A, we need
// department A's departmentID to find its parent, so another where criteria that checks ID == DepartmentID, so we got T and the new list
// basically child from first where parent from second where, and object created.
// for showing the results
foreach (var item in departments)
{
result.Add(new { DepartmentID = item.departmentID,ID = item.ID, level= item.level,Name = item.Name}); // child added to list
result.Add(new { DepartmentID = item.Parent.DepartmentID, ID = item.Parent.ID, level = item.Parent.level, Name = item.Parent.Name }); // parent added to list
}
Result;
2-
List<object> childParent = new List<object>();
// basically get the child first
Departments child1 = entites.Departments.Where(x => x.ID == _ID).FirstOrDefault();
// find parent with child object
Departments parent1 = entites.Departments.Where(x => x.ID == child1.DepartmentID).FirstOrDefault();
// create child object with level
childParent.Add(new { child1.DepartmentID, child1.ID,child1.Name , level = 0});
// create parent object with level
childParent.Add(new { parent1.DepartmentID,parent1.ID,parent1.Name, level = 1 });
Result (not the same image, check column Header Text);
Edit 1:
3-
Another way, by giving ID as input and assuming that ID column is unique, so there will be always 2 values at the array and by returning list, the index of items actually represent their levels. (won't add results because they are same :)).Btw you can also use Union instead of Concat.
var ress = list.Where(x=> x.ID ==2)
.SelectMany(x=> list.Where(c=> c.ID == x.ID).Concat(list.Where(s => s.ID == x.DepartmentID))).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("DepartmentID");
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Level");
for (int i = 0; i < ress.Count(); i++)
{
dt.Rows.Add(ress[i].DepartmentID, ress[i].ID, ress[i].Name, i);
}
dataGridView1.DataSource = dt;
Edit 2
There is not cte in linq, basically using view,sp is the first choise but here is a solution, it might be a little push. Anyway it gives the result.
List<Departments> childParent = new List<Departments>();
// or basically get the child first
Departments child1 = entites.Departments.Where(x => x.ID == 7).FirstOrDefault();
// find parent with child object
Departments parent1 = entites.Departments.Where(x => x.ID == child1.DepartmentID).FirstOrDefault();
// create child object with level
Departments dep = new Departments(); // I add to department class a string level field
dep.DepartmentID = child1.DepartmentID;
dep.ID = child1.ID;
dep.Name = child1.Name;
dep.level = 0; // first item
childParent.Add(dep);
// create parent object with level
dep = new Departments();
dep.DepartmentID = parent1.DepartmentID;
dep.ID = parent1.ID;
dep.Name = parent1.Name;
dep.level = 1; // parent one
childParent.Add(dep);
while (childParent.Select(t => t.DepartmentID).Last() != null) // after added to list now we always check the last one if it's departmentID is null, if null we need to stop searching list for another parent
{
int? lastDepID = childParent.Last().DepartmentID; // get last departmentID
Departments tempDep = entites.Departments.Single(x => x.ID == lastDepID); // find as object
tempDep.level = childParent.Last().level + 1; // increase last level
childParent.Add(tempDep); // add to list
}
(Added another C1 to check 4th level)
Hope helps,
Below is the simple console project Program class code.
You can check with different IDs for the input parameter of the GetParentSet method.
class Program
{
static void Main(string[] args)
{
Program p = new Program();
var result= p.GetParentSet(6);
foreach(var a in result)
{
Console.WriteLine(string.Format("{0} {1} {2}",a.ID,a.Name,a.DepartmentId));
}
Console.Read();
}
private List<Department> GetParentSet(int id)
{
List<Department> result = new List<Department>(); //Result set
using (RamzDBEntities context = new RamzDBEntities())
{
var nodeList = context.Departments.Where(t=>t.ID<=id).ToList(); //Get All the the entries where ID is below or greater than the given to the list
var item = nodeList.Where(a => a.ID == id).SingleOrDefault(); //Get the default item for the given ID
result.Add(item); //Add it to the list. This will be the leaf of the tree
int size = nodeList.Count(); //Get the nodes count
for (int i = size; i >= 1;i--)
{
var newItem= nodeList.Where(j => j.ID == item.DepartmentId).SingleOrDefault(); //Get the immediate parent. This can be done by matching the leaf Department ID against the parent ID
if (item!=null && !result.Contains(newItem)) //If the selcted immediate parent item is not null and it is not alreday in the list
{
result.Add(newItem); //Add immediate parent item to the list
}
if (newItem.ID == 1) //If the immediate parent item ID is 1 that means we have reached the root of the tree and no need to iterate any more.
break;
item = newItem; //If the immediate parent item ID is not 1 that means there are more iterations. Se the immediate parent as the leaf and continue the loop to find its parent
}
}
return result; //return the result set
}
}
Code itself is self-explanatory. However below is the explanation. Hope this will help!
First all the entries with ID below or equal to the given ID is
assigned to a List
Then get the leaf of the tree and add it to the list named result. This is the first element of our result set
We iterate through the retrieved entries descending order. Get the immediate parent of the leaf by equating parent's ID to leaf's department ID
If this immediate parent is not null and its not already in the list add it to the list.
Make the immediate parent item as the leaf and continue the loop so that we can get the parent of the immediate parent.
continue this until we reach the root of the tree.
If the immediate parent ID is=1 that means we have reached the root of the tree and we can break the loop.
Since you generated the edmx, you have the code generated for your DbContext and for your Model Classes including Departments like on this screenshot.
You shouldn't modify them because they might (will) get overwritten by EF tools anyway on any model manipulation. Fortunately both classes are generated as partial so the creators thought about people wanting to customize it safely.
Example below is made for simplicity of implementation not for top performance. I assumed that the table containing Departments is not enormously big and the levels of nesting in hierarchy are not enormously deep.
Create a new Class (*.cs file) in your project and extend your auto-generated Departments class by your custom method or property:
using System;
using System.Collections.Generic;
using System.Linq;
namespace CustomEF.EFStuff
{
public partial class Departments
{
public List<Departments> Hierarchy {
get {
List<Departments> retVal = new List<Departments>();
retVal.Add(this);
using (YourAutoGeneratedContext ctx = new YourAutoGeneratedContext())
{
Departments tmp = this;
while(tmp.DepartmentID != null)
{
tmp = ctx.Departments.First(d => d.ID == tmp.DepartmentID);
retVal.Add(tmp);
}
}
return retVal;
}
private set { }
}
}
}
When you extend the partial class, make sure that you put it in the same namespace. In my case I named my project CustomEF and I've placed the edmx file in the EFStuff subfolder so the generator placed the auto generated class in the CustomEF.EFStuff namespace.
The example above will allow you to get the hierarchy for any Departments object e.g.
int level = 0;
foreach(Departments d in someDepartmentObject.Hierarchy)
{
Console.WriteLine(d.ID.ToString() + ", " + d.DepartmentID.ToString() + ", " + d.Name +", " +(level++).ToString());
}
If you also need to get the hierarchy from some code where you have an ID but not the object, you can additionally create another class (*.cs file) where you'll extend the auto-generated context.
using System.Collections.Generic;
using System.Linq;
namespace CustomEF.EFStuff
{
public partial class YourAutoGeneratedContext
{
public List<Departments> GetDepartmentHierarchy(int departmentId)
{
Departments mydep = this.Departments.FirstOrDefault(d => d.ID == departmentId);
if (mydep == null)
{
throw new System.Data.Entity.Core.ObjectNotFoundException("There is no department with ID = " + departmentId.ToString());
}
return mydep.Hierarchy;
}
}
}
Or in this case you might want to move the implementation to the Context class entirely, without extending the Departments class at all (and you wouldn't have to create an additional instance of your context, you'll have the this to use).
using System.Collections.Generic;
using System.Linq;
namespace CustomEF.EFStuff
{
public partial class YourAutoGeneratedContext
{
public List<Departments> GetDepartmentHierarchy(int departmentId)
{
Departments tmp = this.Departments.FirstOrDefault(d => d.ID == departmentId);
if (tmp == null)
{
throw new System.Data.Entity.Core.ObjectNotFoundException("There is no department with ID = " + departmentId.ToString());
}
List<Departments> retVal = new List<Departments>();
retVal.Add(tmp);
while (tmp.DepartmentID != null)
{
tmp = this.Departments.First(d => d.ID == tmp.DepartmentID);
retVal.Add(tmp);
}
return retVal;
}
}
}
As another unsophisticated use example:
YourAutoGeneratedContext ctx = new YourAutoGeneratedContext();
level = 0;
foreach (Departments currentHier in ctx.GetDepartmentHierarchy(10))
{
Console.WriteLine(currentHier.ID.ToString() + ", " + currentHier.DepartmentID.ToString() + ", " + currentHier.Name + ", " + (level++).ToString());
}
I don't know how much you can trust the data in the database. You might want to implement some checks including cross-referencing departments to prevent infinite loop.
Note that formally the term 'to extend a class' may apply to extension methods rather then to partial classes. I used this word from lack of better one. Extension methods would be something that you might want to use if, for some reason, you'd need your method/property returning EF native DbSet<> instead of the List<>. In such case you might want to take look at: https://shelakel.co.za/entity-framework-repository-pattern/
Example in EF6 to get all parents up to root node.
public class Department
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
public virtual Department Parent { get; set; }
public virtual ICollection<Department> Children { get; set; }
private IList<Department> allParentsList = new List<Department>();
public IEnumerable<Department> AllParents()
{
var parent = Parent;
while (!(parent is null))
{
allParentsList.Add(parent);
parent = parent.Parent;
}
return allParentsList;
}
}
use include keyword.
_context.Invoices.Include(x => x.Users).Include(x => x.Food).ToList();
I have a List where the contents of the list are indented by spaces.
There are two columns (a control type as well as an ID) and the columns are of fixed length.
The indention denotes the parent – child relationship between the elements.
As an example:
Modal 000
Child1 100
Child1 110
Child2 200
Child1 210
Child2 220
Child1 221
Child3 300
To hold the Information I have the following simple classes:
class CustomDefinition
{
public List<CustomControl> Children { get; set; }
}
class CustomControl
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public List<CustomControl> Children { get; set; }
}
The plan is to have an instance of the CustomDefinition class which will contain the root element whilst the other elements, being children of the root element, will be in the children collection of the root itself. The root element will have a ParentId of 000 whereas the children should receive the id of its parent element.
As the children themselves have children, they are to be placed in their own children collection.
I have an Extension method found here which provides me with the index of the first non-space character, but I have no idea how I should continue:
private static void _TestParse(List<string> items)
{
CustomDefinition definition = new CustomDefinition() {
Children = new List<CustomControl>()
};
int currentControlId = 0;
for (int i = 0; i < items.Count; i++)
{
// the current element
var control = items[i];
// the aforementioned extension method
var controlIndex = control.IndexOf<char>(c => !char.IsWhiteSpace(c));
// fixed length for the first column
var controlName = control.Substring(0, 20).Trim();
// fixed length for the second column
var controlId = control.Substring(20, 3);
int.TryParse(controlId, out currentControlId);
definitions.Children.Add(new CustomControl() {
Id = currentControlId,
Name = controlName
});
// i need to find if the current element has children
// (in the example above the root element has 3 children)
// then add these children to the current element
var currentElement = definitions.Children
.Find(x => x.Id == currentControlId);
// then do this for all children and their children?!
}
}
I do hope this all makes sense and I appreciate any help whatsoever.
If I start with this data:
var data = new []
{
"Modal 000",
" Child1 100",
" Child1 110",
" Child2 200",
" Child1 210",
" Child2 220",
" Child1 221",
" Child3 300",
};
Then I can create a list of raw items like this:
var items =
from x in data
let idText = x.Substring(20)
let indentedText = x
.Substring(0, x.Length - idText.Length)
.TrimEnd()
let name = indentedText.Trim()
let indent = indentedText.Length - name.Length
select new
{
id = int.Parse(idText),
name,
indent,
};
And that gives me:
Now I can use this code to turn it into the desired object model:
var itemDictionary = new Dictionary<int, CustomControl>();
foreach (var item in items)
{
itemDictionary[item.indent] = new CustomControl()
{
Id = item.id,
Name = item.name,
Children = new List<CustomControl>(),
};
if (item.indent != 0)
{
itemDictionary[item.indent].ParentId =
itemDictionary[item.indent - 1].Id;
itemDictionary[item.indent - 1]
.Children.Add(itemDictionary[item.indent]);
}
}
I then get this output:
In the Main method I created sample data, initialized your classes, and performed parsing of the data. There is a recursive method that is responsible for searching for parent of the current node. I had to modify your CustomControl class. Looking at the structure of your IDs in the sample it makes more sens to keep them as strings (revert it to int if you prefer them that way - will only require slight modifications of my code). I also added a property to your CustomControl. It's responsible for remembering the indentation level of the item.
More explanations in the code as comments. Below the code:
static void Main(string[] args)
{
// Create sample data
List<string> items = new List<string>();
items.Add("Modal 000");
items.Add(" Child1 100");
items.Add(" Child1 110");
items.Add(" Child2 200");
items.Add(" Child1 210");
items.Add(" Child2 220");
items.Add(" Child1 221");
items.Add(" Child3 300");
// Initialize the collection object
CustomDefinition customDefinition = new CustomDefinition();
customDefinition.Children = new List<CustomControl>();
// Keep reference to the previous item in the list
CustomControl prevItem = null;
for (int i = 0; i < items.Count; ++i)
{
// Find indentation level
int currentIndent = items[i].TakeWhile(c => char.IsWhiteSpace(c)).Count();
string[] itemIdPair = items[i].Trim().Split(' ').ToArray();
// Create CustomControl from data
CustomControl currentItem = new CustomControl();
// Before split the string is trimmed so name is always first item and ID - last one
currentItem.Id = (itemIdPair[itemIdPair.Length -1]);
currentItem.Name = itemIdPair[0];
currentItem.IndentLevel = currentIndent;
// Add CustomControl reference to the collection
customDefinition.Children.Add(currentItem);
// First item - main node
if (prevItem == null)
{
prevItem = currentItem;
}
// Siblings - get the same parent
else if (currentItem.IndentLevel == prevItem.IndentLevel)
{
CustomControl parent = customDefinition.Children.Where(item => item.Id == prevItem.ParentId).FirstOrDefault();
currentItem.ParentId = parent.Id;
if (parent.Children == null)
parent.Children = new List<CustomControl>();
parent.Children.Add(currentItem);
}
// Child
else if (currentItem.IndentLevel > prevItem.IndentLevel)
{
currentItem.ParentId = prevItem.Id;
if (prevItem.Children == null)
prevItem.Children = new List<CustomControl>();
prevItem.Children.Add(currentItem);
}
// Item's level is higher than the previous item - find how far does it go and
// get the parent for the item
else
{
// parent's indentation will be one level higher than the current one (currentIndent - 1)
// try to find it recursively
// Use the previous node in the list of nodes as the starting point
CustomControl parent = GoBackToLevel(customDefinition.Children, prevItem, currentIndent - 1);
currentItem.ParentId = parent.Id;
if (parent.Children == null)
parent.Children = new List<CustomControl>();
parent.Children.Add(currentItem);
}
// Replace the reference to the previous item.
prevItem = currentItem;
}
}
private static CustomControl GoBackToLevel(List<CustomControl> items, CustomControl start, int parentLevel)
{
// Find a parent of the starting CustomControl using the control's ID.
CustomControl parent = items.Where(item => item.Id == start.ParentId).FirstOrDefault();
// Does the parent has the expected indentation level? Yes - get the parent
// else use the parent as the starting point for search instead.
if (parent.IndentLevel == parentLevel)
return parent;
else
return GoBackToLevel(items, parent, parentLevel);
}
And the modified CustomControl:
class CustomControl
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Name { get; set; }
public List<CustomControl> Children { get; set; }
public int IndentLevel { get; set; }
}