Solved

Recursive Nesting LINQ to SQL select statement

Posted on 2008-10-21
1
1,894 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
BToson earned 500 total points
ID: 22871843
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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