How to calculate intermediate values (LINQ to XML)
This article shows how to calculate intermediate values for use in sorting, filtering, and selecting in C# and Visual Basic.
Example: Use the let
clause to calculate based on element data
The following example uses the let
clause to calculate products of numerical values from elements. It uses XML document Sample XML file: Numerical data.
XElement root = XElement.Load("Data.xml");
IEnumerable<decimal> extensions =
from el in root.Elements("Data")
let extension = (decimal)el.Element("Quantity") * (decimal)el.Element("Price")
where extension >= 25
orderby extension
select extension;
foreach (decimal ex in extensions)
Console.WriteLine(ex);
Dim root As XElement = XElement.Load("Data.xml")
Dim extensions As IEnumerable(Of Decimal) = _
From el In root.<Data> _
Let extension = CDec(el.<Quantity>.Value) * CDec(el.<Price>.Value) _
Where extension > 25 _
Order By extension _
Select extension
For Each ex As Decimal In extensions
Console.WriteLine(ex)
Next
This example produces the following output:
55.92
73.50
89.99
198.00
435.00
Example: Calculate from XML that's in a namespace
The following example shows the same query as before, but for XML that's in a namespace. It uses the XML document Sample XML file: Numerical data in a namespace.
For more information, see Namespaces overview.
XElement root = XElement.Load("DataInNamespace.xml");
XNamespace ad = "http://www.adatum.com";
IEnumerable<decimal> extensions =
from el in root.Elements(ad + "Data")
let extension = (decimal)el.Element(ad + "Quantity") * (decimal)el.Element(ad + "Price")
where extension >= 25
orderby extension
select extension;
foreach (decimal ex in extensions)
Console.WriteLine(ex);
Imports <xmlns="http://www.adatum.com">
Module Module1
Sub Main()
Dim root As XElement = XElement.Load("DataInNamespace.xml")
Dim extensions As IEnumerable(Of Decimal) = _
From el In root.<Data> _
Let extension = CDec(el.<Quantity>.Value) * CDec(el.<Price>.Value) _
Where extension > 25 _
Order By extension _
Select extension
For Each ex As Decimal In extensions
Console.WriteLine(ex)
Next
End Sub
End Module
This example produces the following output:
55.92
73.50
89.99
198.00
435.00
See also
Surađujte s nama na GitHubu
Izvor za ovaj sadržaj možete pronaći na GitHubu, gdje možete stvarati i pregledavati probleme i zahtjeve za povlačenjem. Dodatne informacije potražite u našem vodiču za suradnike.