将模式转换为ViewModel

时间:2013-03-28 10:31:54

标签: c#

我,在特殊情况下我不想使用automepper,这是我的模型到ViewModel的转换方法:

public static BlogPostViewModel ConvertToBlogPostViewModel(this BlogPost blogPost)
        {
            var blogPostViewModel = new BlogPostViewModel
                {
                    Body = blogPost.Body,
                    Summary = blogPost.Summary,
                    Title = blogPost.Title
                };

            return blogPostViewModel;
        }

现在我怎么能为IEnumerable做同样的方法()!??

public static IEnumerable<BlogPostViewModel> ConvertToBlogPostViewModelList(this IEnumerable<BlogPost> blogPosts)
        {
            ??

        }

谢谢!

1 个答案:

答案 0 :(得分:3)

以下是您方法的全部内容:

return blogPosts.Select(ConvertToBlogPostViewModel);

这相当于:

return blogPosts.Select(x => ConvertToBlogPostViewModel(x));