Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

linq query on recursive object

Posted on 2013-05-18
3
Medium Priority
?
416 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

670 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