user5841191
user5841191

Reputation:

How to call a method within a lambda expression?

I'm working on a project and I'm stuck by this bulk of code.

I don't understand how CreateEcuDetails method is called from the lambda expression without to specify the parameter...

 var ecuList = new List<EcuDetails>();

 var distinctEcuType = (from c in convertedEcuType
     select c.ShortEcuType).Distinct().ToList();
 ecuList.AddRange(distinctEcuType.Select(CreateEcuDetails).OrderBy(x => x.Name));

 private EcuDetails CreateEcuDetails(string ecuType)
 {
     return new EcuDetails
     {
         Name = ecuType,
         ImportPath = ecuType,
         LogicalPath = "Ecu Type"
     };
 }

This code is already wrote and I have to write something similar to this but CreateEcuDetails will have to get one more parameter which is another string but as I said, I don't know how the method works like that, and when I add the other parameter to the method it doesn't work anymore...

What I want to do is to order the ecuList by two elements, first by carModel and then by ecuType.

So if someone could help me, I'd be very grateful.

Thank you !

Upvotes: 3

Views: 65

Answers (1)

David Arno
David Arno

Reputation: 43264

It's simply using a syntactic sugar feature of allowing you to specify a method group instead of a lambda. So the code:

distinctEcuType.Select(CreateEcuDetails)

effectively gets translated to the following by the compiler:

distinctEcuType.Select(x => CreateEcuDetails(x))

Upvotes: 3

Related Questions