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

  • Home
  • SEARCH
  • 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 340949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:39:49+00:00 2026-05-12T10:39:49+00:00

I am new to LINQ. I need to return the id with the correct

  • 0

I am new to LINQ. I need to return the id with the correct price information for today’s date for each MPrice.

Here is an example of the XML:

<Pricing>
<MPrice>
    <Id>0079</Id>
      <Price>
        <Price>31.25</Price>
        <StartDt>2009-8-01</StartDt>
        <EndDt>2009-08-26</EndDt>
      </Price>
      <Price>
        <ListPrice>131.25</ListPrice>
        <StartDt>2009-08-26</StartDt>
        <EndDt>9999-12-31</EndDt>
       </Price>
   </MPrice>
   <MPrice>
    <Id>0081</Id>
      <Price>
        <Price>131.25</Price>
        <StartDt>2009-8-01</StartDt>
        <EndDt>2009-08-26</EndDt>
      </Price>
      <Price>
        <ListPrice>231.25</ListPrice>
        <StartDt>2009-08-26</StartDt>
        <EndDt>9999-12-31</EndDt>
       </Price>
   </MPrice> 
</Pricing>
  • 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-05-12T10:39:50+00:00Added an answer on May 12, 2026 at 10:39 am

    Here is one way of doing it:

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    class Program
    {
        static void Main()
        {
            String xml = @"<Pricing>
                <MPrice>
                    <Id>0079</Id>
                    <Price>
                    <Price>31.25</Price>
                    <StartDt>2009-8-01</StartDt>
                    <EndDt>2009-08-26</EndDt>
                    </Price>
                    <Price>
                    <ListPrice>131.25</ListPrice>
                    <StartDt>2009-08-26</StartDt>
                    <EndDt>9999-12-31</EndDt>
                    </Price>
                </MPrice>
               </Pricing>";
    
            var priceInfo = from e in XElement.Parse(xml).Elements("MPrice").Elements("Price")
                    let start = DateTime.Parse(e.Descendants("StartDt").FirstOrDefault().Value)
                    let end = DateTime.Parse(e.Descendants("EndDt").FirstOrDefault().Value)
                    where start < DateTime.Now && end > DateTime.Now
                    select new { Id = e.Parent.Element("Id").Value, ListPrice = e.Element("ListPrice").Value };
    
            Console.WriteLine(priceInfo.FirstOrDefault().Id);
            Console.WriteLine(priceInfo.FirstOrDefault().ListPrice);
        }
    }
    

    Output:

    0079
    131.25
    

    Please note that there needs to be much more error checking than this example provides. I would specifically add checking around the parsing of the datetime (perhaps by using a function that wraps DateTime.TryParseExact).

    Edit: If you want to use an XDocument instead of an XElement you will need to make a subtle change to the query (notice the usage of the Descendants method instead of the Elements method):

    var priceInfo = from e in XDocument.Parse(xml).Descendants("MPrice").Elements("Price")
            let start = DateTime.Parse(e.Descendants("StartDt").FirstOrDefault().Value)
            let end = DateTime.Parse(e.Descendants("EndDt").FirstOrDefault().Value)
            where start < DateTime.Now && end > DateTime.Now
            select new { Id = e.Parent.Element("Id").Value, ListPrice = e.Element("ListPrice").Value };
    

    Remember that you don’t need to use an XDocument unless you are working with the XML as a true document. In most cases, the XElement type is sufficient.

    Edit #2: If you want to load the XDocument from disk then use this approach:

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    class Program
    {
        static void Main()
        {
            XDocument document = XDocument.Load(@"d:\test.xml");
    
            var priceInfo = from e in document.Descendants("MPrice").Elements("Price")
                    let start = DateTime.Parse(e.Descendants("StartDt").FirstOrDefault().Value)
                    let end = DateTime.Parse(e.Descendants("EndDt").FirstOrDefault().Value)
                    where start < DateTime.Now && end > DateTime.Now
                    select new { Id = e.Parent.Element("Id").Value, ListPrice = e.Element("ListPrice").Value };
    
            Console.WriteLine(priceInfo.FirstOrDefault().Id);
            Console.WriteLine(priceInfo.FirstOrDefault().ListPrice);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some information here. I am totally new at MVC therefore for you
I'm new to LINQ. I need to compute new_id as follows: public class C_Movement
I am new to linq. My code is as below . I need to
I need my LINQ Query to return the Product Datatype, after being grouped. It
i'm very new to linq to sql and in need of a little assistance.
Linq noob here. I have IList<Product> ApplicableProducts and a IList<Product> CurrentProducts. I need to
still new to the world of linq, and i need some help flatening a
I need to return a number of Linq query results into a List object
There is a proliferation of new LINQ providers. It is really quite astonishing and
New to LINQ.. I am curious as to the syntax to do the following

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.