Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In MS SQL 2005 and up you can create recursive queries. In LINQ to SQL however, you are out of luck. Without restructoring the data in the database, there is no way you can traverse the tree in a single database call.</p> <p>However... there is 1 workaround I can think of. When you are able to group all <code>Category</code> elements of a single tree (or a part of the tree) together, you can pre-load that part of the complete tree in a single statement. After that, you will be able to traverse that part of the tree, without triggering new calls to the database. It would look something like this:</p> <pre><code>// Load the category that will be used as starting point. var subCategory = db.Categories.Single(c =&gt; c.Id == 56); // Performance: Load the complete group in one go. var categories = ( from category in db.Categories where category.GroupId == subCategory.GroupId select category) .ToArray(); // Traverse the tree and get the top-most parent (if any). var parent = subCategory.GetParents().LastOrDefault(); // Extension method to get the parents. public static IEnumerable&lt;Category&gt; GetParents( this Category category) { while (category.Parent != null) { // NOTE: cat.Parent will not cause a database call // when the Parent is already loaded by L2S. yield return cat.Parent; category = category.Parent; } } </code></pre> <p>This of course will only work if you will be able to determine elements as a group. Whether this solution will be faster also depends on the size of the group. When the group of objects that you load (and don't use) is very big, it will actually slow the application down.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload