Solved

linq query on recursive object

Posted on 2013-05-18
3
364 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your answer.  I am a little confused.  Can you please explain the code?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

10 Experts available now in Live!

Get 1:1 Help Now