我有一个列表,试图完成以下操作。我想为列表中的每个项目运行mapper方法......似乎无法使语法正确
var viewModelList = result.MyEnumerable.Select(MyMapper(item goes here))
public static MyViewModel MyMapper(Item item)
{
var viewModel = new MyViewModel();
//do some stuff
return viewModel;
}
答案 0 :(得分:6)
您可以使用:
result.MyEnumerable.Select(r => MyMapper(r));
或使用方法组:
result.MyEnumerable.Select(MyMapper);
答案 1 :(得分:1)
result.MyEnumerable.Select(x => MyMapper(x));
或更多浓缩
result.MyEnumerable.Select(x => new MyViewModel
{
// use x in here
});
答案 2 :(得分:0)
像这样使用
var viewModelList = result.MyEnumerable.Select(s=> MyMapper(s))
public static MyViewModel MyMapper(Item item)
{
var viewModel = new MyViewModel();
//do some stuff
return viewModel;
}