C# 自动映射-如何映射到IEnumerable

C# 自动映射-如何映射到IEnumerable,c#,asp.net-mvc,automapper,C#,Asp.net Mvc,Automapper,我正在制作论坛系统,我有子类别ThreadsViewModel,在其中我试图映射每个线程的最后评论和最后一篇文章的日期。这是我的代码: public class SubCategoryThreadsViewModel : IHaveCustomMappings { public string Title { get; set; } public string Description { get; set; } public IEnumerable<Thread&

我正在制作论坛系统,我有子类别ThreadsViewModel,在其中我试图映射每个线程的最后评论和最后一篇文章的日期。这是我的代码:

public class SubCategoryThreadsViewModel : IHaveCustomMappings
{
    public string Title { get; set; }

    public string Description { get; set; }

    public IEnumerable<Thread> Threads { get; set; }

    public ThreadInfoSubCategoryViewModel ThreadInfoSubCategoryViewModel { get; set; }

    public void CreateMappings(IConfiguration configuration)
    {
        configuration.CreateMap<Thread, SubCategoryThreadsViewModel>()
            .ForMember(m => m.Title, opt => opt.MapFrom(t => t.SubCategory.Title))
            .ForMember(m => m.Description, opt => opt.MapFrom(t => t.SubCategory.Description))
            .ForMember(m => m.Threads, opt => opt.MapFrom(t => t.SubCategory.Threads))
            .ForMember(m => m.ThreadInfoSubCategoryViewModel, opt => opt.MapFrom(t => new ThreadInfoSubCategoryViewModel()
            {
                  LastCommentBy = t.Posts.Select(a => a.Author.UserName),
                  DateOfLastPost = t.Posts.Select(a => a.CreatedOn.ToString()),
            }))
            .ReverseMap();
    }
仅当属性ThreadInfoSubCategory ViewModel不像上面的代码中那样是Ienumerable,并且内部有两个Ienumerable字符串时,才起作用

public class ThreadInfoSubCategoryViewModel
    {
        public IEnumerable<string> LastCommentBy { get; set; }

        public IEnumerable<string> DateOfLastPost { get; set; }
    }
public类threadinfosubcategory视图模型
{
公共IEnumerable LastCommentBy{get;set;}
公共IEnumerable DateOfLastPost{get;set;}
}
这是可行的,但我希望ThreadInfoSubcategory视图模型是Ienumerable,类属性中的字符串是easy foreach


我已经尝试将其设置为IEnumerable,但对于当前的automapper代码,它不起作用。

您需要手动将成员映射到一个
IEnumerable
而不是单个对象

我假设
t.Posts
中的每个
Post
代表一个
threadinfosubcategory视图模型
,因此一个简单的
Select()
应该可以:

public IEnumerable<ThreadInfoSubCategoryViewModel> ThreadInfoSubCategoryViewModel { get; set; }

...

.ForMember(m => m.ThreadInfoSubCategoryViewModel, opt => opt.MapFrom(t =>
    t.Posts.Select(p => new ThreadInfoSubCategoryViewModel()
    {
        LastCommentBy = p.Author.UserName,
        DateOfLastPost = p.CreatedOn.ToString()
    })
))
public IEnumerable ThreadInfoSubcategory视图模型{get;set;}
...
.ForMember(m=>m.threadInfoSubcategory视图模型,opt=>opt.MapFrom(t=>
t、 选择(p=>newthreadinfosubcategory视图模型()
{
LastCommentBy=p.Author.UserName,
DateOfLastPost=p.CreatedOn.ToString()
})
))

这不起作用,它只给我一个日期和最后一条评论,我想知道每个线程的信息
public IEnumerable<ThreadInfoSubCategoryViewModel> ThreadInfoSubCategoryViewModel { get; set; }

...

.ForMember(m => m.ThreadInfoSubCategoryViewModel, opt => opt.MapFrom(t =>
    t.Posts.Select(p => new ThreadInfoSubCategoryViewModel()
    {
        LastCommentBy = p.Author.UserName,
        DateOfLastPost = p.CreatedOn.ToString()
    })
))