Solved

linq query on recursive object

Posted on 2013-05-18
3
374 Views
Last Modified: 2013-05-22
I have an object that has orders.  Each order can have related orders, which can have related orders.....

I need to do a query to find all order-ids (and other details) about the top order and any related orders underneath.


I have this xml format
<orderstatus-list>
 <orderstatus>
   <order-number></order-number>
   <price></price>
   <related-orders>
       <orderstatus>  //this is recursive
         //I removed the duplicate lines         
       </orderstatus>
   </related-orders>
 </orderstatus>
0
Comment
Question by:jackjohnson44
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39178182
so the input is order-number id, and the result is all related order-number ids?
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39178320
i used this xml as example for your case:
<orderstatus-list>
	<orderstatus>
	<order-number>1</order-number>
	<price>1.5</price>
	<related-orders>
		<orderstatus>
		<order-number>2</order-number>
		<price>2.5</price>
		<related-orders>
			<orderstatus> 
			<order-number>3</order-number>
			<price>3.5</price>
			</orderstatus>
		</related-orders>
		</orderstatus>
	</related-orders>
	</orderstatus>
	<orderstatus>
	<order-number>4</order-number>
	<price>4.5</price>
	<related-orders>
		<orderstatus>
		<order-number>5</order-number>
		<price>5.5</price>
		<related-orders>
			<orderstatus> 
			<order-number>6</order-number>
			<price>6.5</price>
			</orderstatus>
		</related-orders>
		</orderstatus>
	</related-orders>
	</orderstatus>
 </orderstatus-list>

Open in new window


here's the code to list all order and their related orders recursively:
        static IEnumerable yourfunction(XElement element)
        {
            yield return new 
            {
                ID = element.Element("order-number").Value,
                Price = element.Element("price").Value
            };

            foreach (XElement e in element.XPathSelectElements("related-orders/orderstatus"))
            {
                foreach (var ee in yourfunction(e))
                {
                    yield return ee;
                }
            }
        }

        static void Main(string[] args)
        {
            var root = XElement.Load(@"C:\temp\1.xml");
            var orders = root.Elements("orderstatus");
            List<IEnumerable> list = new List<IEnumerable>();
            foreach (var order in orders)
            {
                list.Add(yourfunction(order));
            }
        }

Open in new window

the result is list of enumerables.
the first item in each enumerable is the root order, and the related orders are the sequential.
screenshot
0
 

Author Comment

by:jackjohnson44
ID: 39184459
Thanks for your answer.  I am a little confused.  Can you please explain the code?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now