views:

23

answers:

1

Can someone help with an explanation of what this means:

... .Select(Func<XElement, XElement>selector)

Please an example of what should go in as parameter will be appreciated.

Also found it a little bit difficult naming this question. Suggestion will also be appreciated.

+2  A: 

It's a function taking XElement as argument and returning an XElement, so for instance:

public XElement someFunction(XElement argument)
{
    XElement someNewElement = new XElement();
    ... // do something with someNewElement, taking into account argument
    return someNewElement;
}

Func<XElement, XElement> variableForFunction = someFunction;

.... .Select(variableForFunction);

I'm not interely sure if you have to assign it to a variable first, you could probably just do this:

... .Select(variableForFunction);

give it a try (and let me know if it works :) )

oh and for more information, here's the msdn article, it also explains how to use delegates:

Func<XElement, XElement> variableForFunction = delegate(XElement argument)
    {
        ....//create a new XElement
        return newXElement;
    }

and how to use lambdas, for instance:

Func<XElement, XElement> variableForFunction = s => {
    ....;//create an XElement to return
    return newXElement;
}

or, in this instance, use the lambda directly:

.... .Select( s => {
    ....;//create an XElement to return
    return newXElement;
})

edited it following Pavel's comment

Zenon
In general, `selector` for `Select()` should return a new element, rather than mutating the element given to it and returning that. It's also worth mentioning lambdas, since they're most often used in this context, rather than named functions.
Pavel Minaev
+1 This i what im trying to do. i have strengths.Select<XElement, XElement>(someFunc), which from your explanation, it takes in a delegate function. What got me there is that im trying to select an XElement from an object where the attribute matches a particular value. These XElements have a similar name but different ids.
Colour Blend
And i know your answer is the solution to my question, but could you help me out the above comment.
Colour Blend
This ... .Select(someFunction); should have been ... .Select(variableForFunction);. Cause, variableForFunction is the function name.
Colour Blend
edited it. uhm could you tell me what kind of object you are trying to call Select(...) on?
Zenon
Thanks. Don't bother. After looking at the article it have me hope that i was on the right direction. What i just had to do was convert a value to string.
Colour Blend