Code before the changes:
List<ProductBrandModel> model = brands.Select(item => Mapper.Map<ProductBrand, ProductBrandModel>(item)).ToList();
Code after the improvement:
List<ProductBrandModel> model = brands.Select(Mapper.Map<ProductBrand, ProductBrandModel>).ToList();
What is this doing? Is it implicitly running that mapping on every item in the brands
collection?
Since you're directly passing the parameter of the lambda expression to the Mapper.Map
method, it is exactly equivalent to specifying this method directly as the projection for Select
. The signature of Mapper.Map
is compatible with the Func<TSource, TResult>
delegate, so R# suggests to use the method group directly rather than a lambda expression.
The first line creates a method that immediately calls the Mapper.Map function. This is unnecessary since the Mapper.Map method matches the expected definition of Select and can call Mapper.Map directly. Resharper changes it so that only 1 method is called and the extra method is not generated by the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With