0

This is my first question here so please go easy on me ;-)

I am taking a course on Udemy to learn ASP.NET MVC 5. This course is being taught using C# but I have been following along using VB.NET. Right about half-way through the course, the instructor introduces a library called AutoMapper for mapping domain objects to DTOs (data transfer objects).



The C# code I am having trouble with:

return _context.Customers.ToList().Select(Mapper.Map<Customer, CustomerDto>);

In VB.NET I believe it should be:

Return _context.Customers.ToList().[Select](Mapper.Map(Of Customer, CustomerDto))



However I am receiving the following errors:

  • Overload resolution failed because no accessible 'Map' accepts this number of arguments.

  • Overload resolution failed because no accessible '[Select]' can be called with these arguments: Extension method 'Public Function [Select](Of TResult)(selector As Func(Of Customer, TResult)) As IEnumerable(Of TResult)' defined in 'Enumerable': Type parameter 'TResult' cannot be inferred.



The signature of the AutoMapper function being called:

VB.NET

Public Shared Function Map(Of TSource, TDestination)(source As TSource) As TDestination

C#

public static TDestination Map<TSource, TDestination>(TSource source);



It would appear that while C# allows omitting the (TSource source) parameter, VB.NET is requiring the same parameter (source As TSource)

jmjohnson85
  • 347
  • 4
  • 9

1 Answers1

3

The C# construct being used here is called a Method Group.

The construct without a method group in C# would be to use a lambda expression, like this:

list.Select(c => Mapper.Map<Customer,CustomerDto>(c))

Which translates to VB.Net as follows:

list.Select(Function(c) Mapper.Map(Of Customer, CustomerDto)(c))

You can achieve something similar to the C# method group in VB.Net with the AddressOf operator:

list.Select(AddressOf Mapper.Map(Of Customer, CustomerDto))
jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • Holy moly that was fast! Thank you so much!! I spent quite a bit of time on this yesterday. I will now spend some time learning about the Method Group construct as I've been really having a hard time wrapping my mind around what's going on. Thanks again! :-) – jmjohnson85 Aug 16 '18 at 18:32