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();
Related
I have next structure Class with related List (Many - One):
public class ContractorPpeItemHonestSign
{
[Key]
public int Id { get; set; }
...
[InverseProperty("ContractorPpeItemHonestSign")]
public virtual List<IssueHistoryHonestSign> IssueHistoryHonestSigns { get; set; }
}
}
I trying do filter all Classes where List.count == 0 like this:
public class SignsWithFilterAndPaginationSpec : BaseSpecification<ContractorPpeItemHonestSign>
{
public SignsWithFilterAndPaginationSpec(SignsSpecParams signParams)
: base(x => x.ContractorId == signParams.ContractorId
&& x.DeletedDate == null
&& x.IssueHistoryHonestSigns.Count == 0)
{
AddInclude(x => x.IssueHistoryHonestSigns);
ApplyPaging(signParams.PageSize * (signParams.PageIndex - 1), signParams.PageSize);
}
}
}
public class SpcificationEvaluator<TEntity> where TEntity: class
{
public static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery, ISpecification<TEntity> spec)
{
var query = inputQuery;
if (spec.Criteria != null)
{
query.Where(spec.Criteria);
}
if (spec.OrderBy != null)
{
query = query.OrderBy(spec.OrderBy);
}
if (spec.OrderByDesc != null)
{
query = query.OrderByDescending(spec.OrderByDesc);
}
if (spec.IsPaginataionEnabled)
{
query = query.Skip(spec.Skip).Take(spec.Take);
}
query = spec.Includes.Aggregate(query, (currentEntity, include) => currentEntity.Include(include));
return query;
}
}
But it doesn't work ;((( Why and how can i achieve this goal?
Also i can to filter a result in memory, because all includes are done.. But it isn't my case.
Also i think that WHERE clause doesn't work too ...
You wrote:
I trying do filter all Classes where List.count == 0
I think you meant the following:
Requirement: Given an IQueryable<ContractorPpeItemHonestSign>, give me all ContractorPpeItemHonestSign that has no IssueHistoryHonestSigns at all.
How about this:
IQueryable<ContractorPpeItemHonestSign> inputItems = ...
var inputItemsWithoutHonestSigns = inputItems
.Where(inputItem => !inputItem.IssueHistoryHonestSigns.Any());
In words: from the sequence of ContractorPpeItemHonestSigns in object inputItems, keep only those ContractorPpeItemHonestSign that have no IssueHistoryHonestSigns at all.
Entity framework will do a GroupJoin of tables ContractorPpeItemHonestSign and IssueHistoryHonestSigns, and keep only those ContractorPpeItemHonestSign that have no IssueHistoryHonestSigns at all.
If you want to do the GroupJoin yourself, join on the foreign key IssueHistoryHonestSign.ContractorPpeItemHonestSignId:
IQueryable<ContractorPpeItemHonestSign> contractors = ...
IQueryable<IssueHistoryHonestSigns > issues = ...
var contractorsWithoutIssues = contractors.GroupJoin(issues,
contractor => contractor.Id, // from every contractor take the primary key
issue => issue.ContractorPpeItemHonestSignId, // from every issue take the foreign key to contractor
// parameter resultSelector, from every contractor with its zero or more issues
// make one new:
(contractor, issuesOfThisContractor) => new
{
// select all Contractor properties that you plan to use
Id = contractor.Id,
...
HasIssues = issuesOfThisContractor.Any(),
})
Where(contractor => !contractor.HasIssues);
A third method:
IQueryable<ContractorPpeItemHonestSign> contractors = ...
IQueryable<IssueHistoryHonestSigns > issues = ...
var contractorsWithoutIssues = contractors
.Where(contractor => !issues.Where(issue => issue.ContractorPpeItemHonestSignId == contractor.Id)
.Any());
In words: from all contractors, keep only those contractors where there isn't any issues that has a foreign key that refers to the primary key of the contractor.
Please find below the Cypher Query (which is normal)
MATCH (a:`Entity`) - [r] - (b:`Entity`)
RETURN a , r , b
First Question :
How do I translate this to neo4jClient in my c# Code.
Second Question :
The Relation has multiple properties. How do I access them as well using neo4Jclient ?
Entity has Properties EntityName and DataSpaceName
The Relation has Properties RelationType and RelationFrequency.
The Code I have used for fetching the node details, is as below
var query1 = client
.Cypher
.Match("(a:`Entity`) - [r] - (b:`Entity`)")
.Where("a.DataSpace = b.DataSpace")
.Return((a,b) => new {
FromEntity = Return.As<string>("a.EntityName"),
ToEntity=Return.As<string>("b.EntityName")
}
);
EDIT :
I had tried to figure it. The following worked.
var query1 = client
.Cypher
.Match("(a:`Entity`) - [r] - (b:`Entity`)")
.Where("a.DataSpace = b.DataSpace")
.Return((a,b) => new {
FromEntity = Return.As<string>("a.EntityName"),
ToEntity=Return.As<string>("b.EntityName"),
Relation=Return.As<string>("r.RelType"),
}
);
However, if the relation is a wild card, namely [*] or [r *1..3], how will I fetch the properties of the relation.
Relationship objects are the same as node objects, so you get them in the same way.
How have you actually got your elements set up? What is an 'Entity', assuming you have a class like:
public class Entity {
public string DataSpace { get; set; }
public string EntityName { get; set; }
}
and a Relationship object:
public class RelationshipObj {
public string RelType { get; set; }
}
You would return with something like this:
var originalQuery = graphClient
.Cypher
.Match("(a:`Entity`)-[r]-(b:`Entity`)")
.Where("a.DataSpace = b.DataSpace")
.Return((a, b, r) => new
{
FromEntity = a.As<Entity>().EntityName,
ToEntity = b.As<Entity>().EntityName,
Relation = r.As<RelationshipObj>().RelType,
});
In the situation where you have [r*1..3] you end up with a more complex query as you're returning an enumeration instead. So you're return for 'Relation' becomes:
Relation = r.As<IEnumerable<RelationshipObj>>()
Which you can no longer pull the 'RelType' property from without parsing the results later:
foreach (var result in results)
{
foreach (var relationship in result.Relation)
{
Console.WriteLine(relationship.RelType);
}
}
Now, you actually ask how to cope with a wildcard relationship, obviously you can't get the properties as above - as you don't actually know what you're asking for. You can get the results as a string and use JSON.NET to parse it to the right object. However, you can also return RelationshipInstance<Dictionary<string,string>> which will give you the TypeKey for the relationship, which you can use to deserialize into your relationship object:
var query = graphClient.Cypher
.Match("(a:`Entity`)-[r*]-(b:`Entity`)")
.Where("a.DataSpace = b.DataSpace")
.Return((a, b, r) => new
{
FromEntity = a.As<Entity>().EntityName,
ToEntity = b.As<Entity>().EntityName,
Relation = r.As<IEnumerable<RelationshipInstance<Dictionary<string,string>>>>(),
});
var results = query.Results.ToList();
foreach (var result in results)
{
foreach (var relationship in result.Relation)
{
if (relationship.TypeKey == "REL_TO")
{
var obj = JsonConvert.DeserializeObject<RelationshipObj>(JsonConvert.SerializeObject(relationship.Data));
Console.WriteLine(obj.RelType);
}
}
}
We have to do a bit of a weird Deserialize/Serialize thing as we can't just use RelationshipObject<string> due to a constraint on the the generic part.
3 Tables:
ParentTable: ParentID (assume there's a ParentID = 5)
ParentChildrenTable: ParentID, ChildrenID (assume there are 3 relation rows of ParentID = 5)
ChildrenTable: ChildrenID, ChildrenName (assume there are 3 children of ParentID = 5, for example: A,B,C)
im trying to do something like "get all children of ParentID=5 and print their names"
using Entity Framework and LinQ
using pseudo-like this is what i mean:
Parent fifthParent = db.ParentTable.FirstOrDefault(p => p.ParentID == 5);
foreach (ParentChildren parentChildren in fifthParent.ParentChildren) // will iterate 3 times
{
//get each child seperatly according
foreach(Child child in parentChildren.Children)
{
//print A (on 1st iteration)
//print B (on 2nd iteration)
//print C (on 3rd iteration)
}
}
as far as i can see it it should be 2 for-loops, though im heavy-struggling with that in the last 2 hours.
Hope u could please provide code samples because I still can not grasp the principle of these queries.
You can use SelectMany to flatten the inner collection:
Parent fifthParent = db.ParentTable.FirstOrDefault(p => p.ParentID == 5);
var children = fifthParent.ParentChildren.SelectMany(c=>c.Children)
foreach (Child parentChildren in children)
{
//print children.
}
This will join everything together and filter it to only return children whose parent's ID is 5.
var childrenOfFifthParent =
from parent in context.ParentTable
join parentChild in context.ParentChildrenTable on parent.ParentID
equals parentChild.ParentID
join child in context.ChildrenTable on parentChild.ChildID
equals child.ChildID
where parent.ParentID == 5
select child;
Then you can do something like:
foreach (var child in childrenOfFifthParent.ToList())
{
// print the child
}
I would start the other way around:
foreach ( var child in db.ChildrenTable
.Where( c => c.ParentChildren.Any( pc => pc.ParentID == 5 ) ) )
{
var foo = child.Name // or whatever else
}
Your ParentChildrenTable class should look something like this
public class ParentChildrenTable
{
public int Id { get; set; }
public int ParentId { get; set;}
public int ChildId {get; set; }
public virtual ParentTable Parent { get; set; }
public virtual ChildrenTable Child { get; set; }
}
Which means inside your first loop you could just access the Child property of the ParentChildrenTable object:
foreach (ParentChildren parentChildren in fifthParent.ParentChildren) // will iterate 3 times
{
ChildrenTable child = parentChildren.Child;
//print A (on 1st iteration)
//print B (on 2nd iteration)
//print C (on 3rd iteration)
}
UPDATE:
To do this with a single LINQ query you can use SelectMany and then a Select call:
var children = db.ParentTable.Where(p => p.ParentID == 5)
.SelectMany(p => p.Children)
.Select(pc => pc.Child);
Or you can do it starting with the children:
var children = db.ChildrenTable.Where(c => c.ParentChildren.Any(pc => pc.ParentId == 5));
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
I have the following table structure it might not be a correct structure but unfortunatly that's what I was given.
id | Name | Parent | Status
1 First 0 Active
2 Child 1 Active
3 2Child 2 Inactive
Logic:
Load Root by Parent = 0 and Status
OnPopulate load child by parent ID and status for every levels after root
my issue is if the status is "Inactive" and I want to see what options are inactive I can't because the first 2 options are active. What I need is to be able to view in my treeview all the levels down to the option that is Inactive or Active.
I have tried the following sql statement
select distinct
m.Id,
m.Name,
m.Parent,
m.[Status]
from mytable m
where m.Parent = 3 and m.[Status] = 'I'
union
select
Id,
Name,
Parent,
[Status]
from mytable
where ID in(select distinct
o.ID
from mytable o
where o.ID = 3 and o.[Status] = 'I') and Parent = 3
I have ran out of ideas in sql and coding to figure this out..hope someone could guide me in the right direction..thanks
Also tried this in code:
protected void mytree_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
//this is just a class that loads the values from db
MYList templist = new ListSyFamily();
templist.LoadAll();//(ddlStatus.SelectedValue, Convert.ToInt32(e.Node.Value));
foreach (temp temp in templist)
{
if (temp.Status == ddlStatus.SelectedValue && temp.Parent == Convert.ToInt32(e.Node.Value))
{
TreeNode child = new TreeNode();
child.Text = temp.Description;
child.Value = temp.Id.ToString();
if (child.ChildNodes.Count == 0)
child.PopulateOnDemand = true;
child.ToolTip = "Ver sub-opciones";
//child.SelectAction = TreeNodeSelectAction.SelectExpand;
child.CollapseAll();
e.Node.ChildNodes.Add(child);
}
}
}
Here is how we handle this.
Assume that you have a class called MyRecord to hold each row of data from the DB:
public class MyRecord
{
public int Id {get; set; }
public int ParentId {get; set; }
public string Name { get; set; }
public string Status { get; set; }
// The children of this node
public MyRecordCollection Children = new MyRecordCollection();
}
Then you have a collection type to hold these records indexed by their id:
public class MyRecordCollection : System.Collections.Generic.Dictionary<int, MyRecord>
{
}
Here is the code (retrieval from the DB not shown) to preprocess the records and then add them to the tree:
MyRecordCollection cAllRecords;
MyRecordCollection cParentRecords = new MyRecordCollection();
// This is a method that just loads the records
cAllRecords = LoadAllRecords();
// Cycle through each of the records
foreach (MyRecord oRecord in cAllRecords.Values)
{
if (oRecord.Id == 0)
{
// If the record is a parent record, add it to the list of parents
cParentRecords.Add(oRecord.Id, oRecord);
}
else
{
// Otherwise, add the current record to its parent's list of children
cAllRecords[oRecord.ParentId].Children.Add(oRecord.Id, oRecord);
}
}
AddNodesToTree(cParentRecords, this.treeView1.Nodes);
And finally, the recursive method for adding the records to the tree:
/// <summary>
/// A recursive method to add all of the records to the specified collection of nodes
/// </summary>
/// <param name="cRecords"></param>
/// <param name="cNodes"></param>
private void AddNodesToTree(MyRecordCollection cRecords, TreeNodeCollection cNodes)
{
foreach (MyRecord oRecord in cRecords.Values)
{
TreeNode oNode = new TreeNode();
oNode.Text = oRecord.Name;
oNode.Tag = oRecord;
cNodes.Add(oNode);
// Now add the node's children if any
if (oRecord.Children.Count != 0)
{
AddNodesToTree(oRecord.Children, oNode.Nodes);
}
}
}
Well, If I were you I would just load the entire table into memory into a simple Collection of a light DTO class and work out your treeview in C#. That seems a lot easier than to try a lot of options in SQL.