Solved

Recursive Nesting LINQ to SQL select statement

Posted on 2008-10-21
1
1,899 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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