?
Solved

Recursive Nesting LINQ to SQL select statement

Posted on 2008-10-21
1
Medium Priority
?
1,901 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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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