Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:55:58+00:00 2026-06-09T02:55:58+00:00

I’m new to linq and I’m having trouble writing a query that pulls back

  • 0

I’m new to linq and I’m having trouble writing a query that pulls back the data I’m looking for. The xml file has orders, each order has purchase order info and products, each prouct has its own elements and descendants. I need to query all the orders and their descendants into one colletion but for some reason the linq query syntax is very counter-intuitive to me.

Here’s a truncated sample of my xml

<fulfillment>
   <orders>
      <order>
         <isbulk>true</isbulk> 
         <purchaseorder>
            <id>Acme Inustries</id> 
            <quantity>15</quantity> 
         </purchaseorder>
         <items>
            <item>
               <prods>
                  <prod>
                     <seq>1</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>xyz123</proxy> 
                        <servicecode>55</servicecode> 
                     </loop>
                  </prod>
                  <prod>
                     <seq>2</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>abc987</proxy> 
                        <servicecode>121</servicecode> 
                     </loop>
                  </prod>
               </prods>
            </item>
         </items>
      </order>
      <order>
         <isbulk>true</isbulk> 
         <purchaseorder>
            <id>ABC Co</id> 
            <quantity>10</quantity> 
         </purchaseorder>
         <items>
            <item>
               <prods>
                  <prod>
                     <seq>1</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>xyz456</proxy> 
                        <servicecode>998</servicecode> 
                     </loop>
                  </prod>
                  <prod>
                     <seq>2</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>abc654</proxy> 
                        <servicecode>664</servicecode> 
                     </loop>
                  </prod>
               </prods>
            </item>
         </items>
      </order>
   </orders>
</fulfillment>

My objects look a bit like this:

public class order
{
    public bool IsBulk { get; set; }

    public PurchaseOrder PurchaseOrder = new PurchaseOrder();
    public List<prod> ListOfProds = new List<prod>();
}

public class prod
{
    public string Seq { get; set; }
    public string IssueType { get; set; }

    public string Proxy { get; set; }
    public string ServiceCode { get; set; }
}

public class PurchaseOrder
{
    public string ID { get; set; }
    public string Quantity { get; set; }
}

So I’ve been working on a query for the best part of a day and just can’t seem to get it right. Here’s what I’ve got so far:

List<order> orderlist = new List<order>();
XDocument xmlDoc = XDocument.Load(FilePath);

var list = (from myOrder in xmlDoc.Descendants("order")             
select new       
{
   linq_orderIsBulk = Convert.ToBoolean(myOrder.Element("isbulk").Value),

   linq_purchaseOrderID = myOrder.Element("purchaseorder").Element("id").Value,
   linq_purchaseOrderQuantity = myOrder.Element("purchaseorder").Element("quantity").Value,

   prodlist = myOrder.Element("items").Element("item").Element("prods").Elements("prod").Select(e => new
   {                
      Linq_seq = e.Element("seq").Value,
      Linq_IssueType = e.Element("issuetype").Value,

      Linq_proxy = e.Element("loop").Element("proxy").Value,
      Linq_serviceCode = e.Element("loop").Element("servicecode").Value
   }).ToList()
});

//do code to put the collection in list into List orderlist

But when I do this, I seem to end up getting an “Object reference not set to an instance of an object.” error on the subquery. When I comment out the Linq_proxy and Linq_serviceCode lines, I get results but not the right ones. When I loop through list and grab a single order then look at the prodlist for that order, the count is the total amount of prods for that file (4) instead of the 2 for that order. Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:56:00+00:00Added an answer on June 9, 2026 at 2:56 am

    I just ran this, and it works fine:

    var orders = new List<order>(
        from myOrder in xmlDoc.Descendants("order")
        let purchaseOrder = myOrder.Element("purchaseorder")
        select new order {
            IsBulk = Convert.ToBoolean(myOrder.Element("isbulk").Value),
            PurchaseOrder = new PurchaseOrder {
                ID = purchaseOrder.Element("id").Value,
                Quantity = purchaseOrder.Element("quantity").Value
            },
            ListOfProds = new List<prod>(
                from product in myOrder.Descendants("prod")
                let loop = product.Element("loop")
                select new prod
                {                
                    Seq = product.Element("seq").Value,
                    IssueType = product.Element("issuetype").Value,
                    Proxy = loop.Element("proxy").Value,
                    ServiceCode = loop.Element("servicecode").Value
                }
            )
        }
    );
    

    Notice that you can project a collection directly into objects of your type, so you don’t have to have code later on to create your orders collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.