Search code examples
c#linqenumerable

Apply method to all elements in enumerable with LINQ


I have a list, trying to accomplish the following. I want to run a mapper method for each item in the list...can't seem to get the syntax correct

var viewModelList = result.MyEnumerable.Select(MyMapper(item goes here))

 public static MyViewModel MyMapper(Item item)
        {
            var viewModel = new MyViewModel();
            //do some stuff
            return viewModel;
        }

Solution

  • You can either use:

    result.MyEnumerable.Select(r => MyMapper(r));
    

    or use a method group:

    result.MyEnumerable.Select(MyMapper);