0

我有一个清单,试图完成以下工作。我想为列表中的每个项目运行一个映射器方法......似乎无法获得正确的语法

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

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

3 回答 3

6

您可以使用:

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

或使用方法组:

result.MyEnumerable.Select(MyMapper);
于 2013-05-24T11:16:06.853 回答
1
result.MyEnumerable.Select(x => MyMapper(x));

或更浓缩

result.MyEnumerable.Select(x => new MyViewModel
{
    // use x in here
});
于 2013-05-24T11:15:36.327 回答
0

像这样使用

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

 public static MyViewModel MyMapper(Item item)
    {
        var viewModel = new MyViewModel();
        //do some stuff
        return viewModel;
    }
于 2013-05-24T11:16:18.317 回答