Reputation: 4414
I need to have a database that starts with a table called "User" that needs to self reference itself and will have a very deep graph of related objects. It will need to be like the left side of the image below (disregard the right side).
I will also need to traverse through this graph both up and downwards in order to calculate percentages, totals, etc. In other words I'll need to travese the entire graph in some cases.
Is this possible and/or how is it done? Can traversing be done right in the LINQ statement? Examples?
EDIT: I'm basically trying to create a network marketing scenario and need to calculate each persons earnings.
Examples:
Hopefully that helps :S
Upvotes: 4
Views: 1501
Reputation: 244968
You can't traverse the whole tree using just LINQ in a way that would translate to single SQL query (or a constant count of them). You can do it either with one query for each level or with one query, that is limited to a specific count of levels (but such a query would get really big with many levels).
In T-SQL (I assume you're using MS SQL Server), you can do this using recursive common table expressions. It should be possible to put that into a stored procedure that you can use from LINQ to get the information you actually want.
To sum up, your options are:
Upvotes: 3
Reputation: 52
I know this is late, but if you look at Directed Graph algorithms, you can bypass the recursive issues. check out these 2 articles:
http://www.sitepoint.com/hierarchical-data-database/
http://www.codeproject.com/Articles/22824/A-Model-to-Represent-Directed-Acyclic-Graphs-DAG-o
Upvotes: 0