dazkraz
asked on
Recursive Nesting LINQ to SQL select statement
Hi. I have an SQL table like:
tblCategories
- fldCategoryID
- fldCategoryName
- fldCategoryParentID
and fldCategoryParentID is the foreign key for fldCategoryID for an unlimited number of nesting levels in the database.
I'm trying to use LINQ to SQL to select this table and produce this...
<root>
<category id="1" name="root category">
<category id="2" name="child of root">
<category id="3" name="child of previous child">
<category id="4" name="deepest nested child">
</category>
</category>
</category>
</categroy>
<category id="55" name="another root category">
</category>
</root>
My LINQ code looks like this so far (but is not complete):
XElement xml=XElement("root",
from c in dataContext.TblCategories
select new XElement("category",
new XElement("id",c.FldCategor yID),
new XElement("name",c.FldCateg oryName)
)
);
How can I generate XML that nests instead of just selecting all categories in a single XML level?
tblCategories
- fldCategoryID
- fldCategoryName
- fldCategoryParentID
and fldCategoryParentID is the foreign key for fldCategoryID for an unlimited number of nesting levels in the database.
I'm trying to use LINQ to SQL to select this table and produce this...
<root>
<category id="1" name="root category">
<category id="2" name="child of root">
<category id="3" name="child of previous child">
<category id="4" name="deepest nested child">
</category>
</category>
</category>
</categroy>
<category id="55" name="another root category">
</category>
</root>
My LINQ code looks like this so far (but is not complete):
XElement xml=XElement("root",
from c in dataContext.TblCategories
select new XElement("category",
new XElement("id",c.FldCategor
new XElement("name",c.FldCateg
)
);
How can I generate XML that nests instead of just selecting all categories in a single XML level?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.