Вопрос

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;
        }
Это было полезно?

Решение

You can either use:

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

or use a method group:

result.MyEnumerable.Select(MyMapper);

Другие советы

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

or more condensed

result.MyEnumerable.Select(x => new MyViewModel
{
    // use x in here
});

Use like this

var viewModelList = result.MyEnumerable.Select(s=> MyMapper(s))

 public static MyViewModel MyMapper(Item item)
    {
        var viewModel = new MyViewModel();
        //do some stuff
        return viewModel;
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top