Link to home
Start Free TrialLog in
Avatar of dazkraz
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.FldCategoryID),
      new XElement("name",c.FldCategoryName)
    )
);

How can I generate XML that nests instead of just selecting all categories in a single XML level?
ASKER CERTIFIED SOLUTION
Avatar of BToson
BToson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial