Reputation: 1037
My xml file looks like this
<xxx>
<fff>1</fff>
</xxx>
<xxx>
<fff>1</fff>
</xxx>
And I want to find the sum of values in nodes . I was trying like this:
var sum = from c in el.Elements()
select new {I don't know what to write here}
and then iterating through sum, but I want to do it in one query.
How the query will be look like?
Upvotes: 1
Views: 972
Reputation: 43021
For the exception case you need to use TryParse not Parse, e.g.
int sum = el.Descendants("fff").Sum (e =>
{
int v;
return int.TryParse(e.Value, out v) ? v : 0;
});
Upvotes: 0
Reputation: 16423
I think it will be more like this:
int sum = root.Descendants("fff").Sum(e => int.Parse(e.Value));
Upvotes: 3
Reputation: 6719
Maybe something like this?
int sum = root.Descendants("fff").Sum(e => (int) e);
Upvotes: 5