Solved

linq query on recursive object

Posted on 2013-05-18
3
408 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
[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
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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