tags:

views:

63

answers:

1

I was basically wondering if it is possible to optimize this code:

AmountComments = Int32.Parse(r.Attribute("AmountComments").Value)

Ideally I would want to write something like

AmountComments = r.Attribute("AmountComments")

r would be a xml tag of type XElement, which is selected before in a Linq query.

+6  A: 

Consider writing an extension method(s) for the type of .Attribute()

One extension method for each type that you'd like out. This way you'd simply be able to:

AmountComments = r.Attribute("AmountComments").ToInt32();


public static class LinqUtils
{
    public static int Int32(this XAttribute attribute)
    {
        return System.Int32.Parse(attribute.Value);
    }
}
p.campbell
Cool thanks, added my solution to your answer.
Tomh
@Tomh - Cheers, was just filling in the extension method myself when I got the orange popup bar saying you'd edited the answer. Good edit!
p.campbell