8

enter image description here

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?

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
  • I also faced the same issue with my below code while understanding TPL: `private static void ParallelForEach() { Parallel.Invoke(() => Method1(), () => Method2()); } private static void Method1() { //do some work } private static void Method2() { //do some work }` – RBT Feb 28 '16 at 02:56

2 Answers2

10

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.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • You have three upvotes but I have more questions. :) I understand the signature is but what is being projected when I use the improved code? Does the .Select burp up each `ProductBrand` object and Mapper.Map assumes that bit is the TSource? – Only Bolivian Here Aug 24 '11 at 21:03
  • 1
    Well, `TSource` is already known, since `brands` is a collection of `ProductBrand`. The compiler infers `TResult` from the return type of `Mapper.Map`. – Thomas Levesque Aug 24 '11 at 21:08
3

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.

NotDan
  • 31,709
  • 36
  • 116
  • 156