3

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 !

1 Answer 1

3

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.