How to optimize LINQ-to-SQL for recursive queries?
I have the following SQL table:
ObjectTable
--------------------------------------------------
| ID | Name | Order | ParentID |
| 开发者_高级运维 int PK | nvarchar(50) | int | int FK |
ObjectTable.ParentID
is a nullable field with a relationship to a different Object record's ID. LINQ-to-SQL generates an class that looks like:
public class DbObject{
int ID { get; set; }
string Name { get; set; }
int Order { get; set; }
int? ParentID { get; set; }
DbObject Parent { get; set; }
EntitySet<DbObject> ChildObjects { get; set; }
}
When I load a DbObject
instance, I need to be able to recursively access the child objects, so that I can write the data to a hierarchical JSON object.
Will I execute a separate query everytime I access the elements via
DbObject.ChildObjects
? Since DB transactions take the longest, it seems like this is a very inefficient way to load a recursive hierarchy.What is the best practice for executing a recursive query with LINQ-to-SQL and/or EF?
Is LINQ-to-SQL integrated with Common Table Expressions?I found Common Table Expression (CTE) in linq-to-sql?
in our case we have created stored procedures with CTE's and put them on the l2s designer and they work like any other Table except they behave as a method rather than property and we have experienced no problem with that so far.
精彩评论