Solved

Recursive Nesting LINQ to SQL select statement

Posted on 2008-10-21
1
1,891 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

774 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