Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

linq query on recursive object

Posted on 2013-05-18
3
Medium Priority
?
418 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:Meir Rivkin
ID: 39178182
so the input is order-number id, and the result is all related order-number ids?
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

971 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