Set the Text
property of button1 to Untyped, and then double-click the button to view its Click event. Enter the
following code in the button1 Click event:
var order = XElement.Load("C:\\Wrox\\AppendixC\\Orders.xml");
var total = (from salesOrder in order.Elements("OrderDetail")
from item in salesOrder.Elements("Item")
347
Appendix C: LINQ to XSD
select (double)item.Element("UnitPrice")
* (int)item.Element("OrderQuantity")
).Sum();
textBox1.Text = total.ToString();
Figure C-1
Figure C-2
348
Appendix C: LINQ to XSD
From the Build menu, select Build Solution to ensure that the project compiles. Then run the application
and click the Untyped button. The text box should be populated with the value of 2280.63, as shown in
Figure C-3.
Figure C-3
This example is similar to the examples you worked with in the Chapters 5 through 9 in the LINQ to
XML section. It uses the Load method of the XElement method to load an XML document into memory.
A LINQ query expression is then executed against the XML document, using the sum( ) query operator to
sum all the order totals.
Pages:
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540