2

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?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
John Doe
  • 3,053
  • 17
  • 48
  • 75
  • It is possible by method group conversion. [This SO](http://stackoverflow.com/questions/886822/what-is-a-method-group-in-c) should explain you more. – Siva Gopal Nov 23 '15 at 20:10

1 Answers1

7

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))
Lee
  • 142,018
  • 20
  • 234
  • 287