Deal all,
Assume I have a XML file and I need to parse it to objects (DTOs). Example:
<Root>
<Item>
<X>1</X>
<Y>2</Y>
<Item>
</Root>
And I have a DTO object:
public class Item
{
public int X{get;set;}
public int Y{get;set;}
public int Z{get;set;}
}
To create a Item object, I need to know X and Y, and I will set Z = X*Y
I use LINQ to parse XML to objects:
XDocument reportDoc = XDocument.Load(@"Report.xml");
var query = from item in reportDoc.Element("Root").Descendants()
select new Item()
{
X = Convert.ToInt32(item.Element("X").Value),
Y = Convert.ToInt32(item.Element("Y").Value)
// Z = X*Y -> I can't do this by this statement
};
Please help me how to set value to Z property directly in LINQ select statement. Thanks.
You can use the
letclause to declare intermediate variables: