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?