John Doe
John Doe

Reputation: 3243

Calling a method from a LINQ select

I am trying to understand how this method call works in a Linq statement. I have a line of code such as:

foreach (var model in myDataList.Select(RenderMyData))
{
    pPoint.CreateStuff(model, true);
}

and RenderMyData looks like this:

    protected PowerPoint.MyModel RenderMyData(CustomData myData)
    {
        // Do stuff
    }

How does the CustomData object get passed to the RenderMyData method? If I wanted to add another parameter to the RenderMyData method (like a bool) then how I can pass that in the linq select?

Upvotes: 2

Views: 593

Answers (1)

Lee
Lee

Reputation: 144206

There is an implicit conversion from a method group (RenderMyData) to a compatible delegate type (Func<CustomData, MyModel> in this case). It is equivalent to:

var model in myDataList.Select(d => RenderMyData(d))

if you add a parameter you can do:

var model in myDataList.Select(d => RenderMyData(d, otherParam))

Upvotes: 7

Related Questions