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>
Here is one way of doing it:
Output:
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
XDocumentinstead of anXElementyou will need to make a subtle change to the query (notice the usage of theDescendantsmethod instead of theElementsmethod):Remember that you don’t need to use an
XDocumentunless you are working with the XML as a true document. In most cases, theXElementtype is sufficient.Edit #2: If you want to load the
XDocumentfrom disk then use this approach: