Solved

Recursive Nesting LINQ to SQL select statement

Posted on 2008-10-21
1
1,887 Views
Last Modified: 2013-11-11
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?
0
Comment
Question by:dazkraz
1 Comment
 
LVL 4

Accepted Solution

by:
BToson earned 500 total points
Comment Utility
Hi,

Personally I would do it this way instead of using a select to xelement:
var xout = new XElement("root", mapcategory(dataContext.tblCategories.ToList(), 0));

Relevevant code below.
			public static List<XElement> mapcategory(List<tblCategory> categories, int? parentid)

			{

				 var xreturn = new List<XElement>();

				 foreach (var category in categories.Where(c => c.fldCategoryParentID == parentid))

				 {

						var xcategory = new XElement("category", new XAttribute("id", category.fldCategoryID), new XAttribute("name", category.fldCategoryName));

						if (categories.Where(c => c.fldCategoryParentID == category.fldCategoryID).Count() > 0) xcategory.Add(mapcategory(categories, category.fldCategoryID));

						xreturn.Add(xcategory);

				 }

				 return xreturn;

			}

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now