简体   繁体   English

当LINQ语句包含多个.include时使用AutoMapper

[英]Using AutoMapper when having a LINQ statement with multiple .Include

So I am new to using AutoMapper and have been able to get basic mapping of items no problem with using LINQ statements that do not use the .Include("blah"), however when I have a statement for example like this; 因此,我对使用AutoMapper并不陌生,并且能够使用不使用.Include(“ blah”)的LINQ语句来获取项目的基本映射,但是当我有这样的示例时;

var courses = dc.Courses.Include("Students")
                        .Include("CourseTimes")
                        .OrderBy(n=>n.CourseSemester.courseStart);

AutoMapper doesnt seem to pull any of the information from ("Students") or ("CourseTimes"). AutoMapper似乎没有从(“ Students”)或(“ CourseTimes”)中提取任何信息。 My objects are posted below and to give a quick breakdown, Courses contain a List of Students(I need Students so I can count the number of people in each course), Courses also contain a List of CourseTimes(so I can display the times of each class for the given course). 我的对象张贴在下面,并提供快速细目分类,课程包含学生列表(我需要学生,所以我可以计算每门课程的人数),课程还包含课程时间列表(因此我可以显示以下时间)给定课程的每个课程)。 Here is my ViewModel that I am using. 这是我正在使用的ViewModel。

public class UserIndexCourseList
{
    [Key]
    public int courseId { get; set; }
    public string courseCode { get; set; }

    public string courseName { get; set; }

    // this simply stored a count when I did Students.Count without using AutoMapper
    public int size { get; set; } 

    public string room { get; set; }

    public List<CourseTime> courseTimeSlot { get; set; }

}

Here are some of the AutoMapper statements I tried to used but had no luck with it working. 这是我尝试使用的一些AutoMapper语句,但运气不好。

//to the viewmodel
        Mapper.CreateMap<Models.Course, ViewModels.UserIndexCourseList>();
        Mapper.CreateMap<Models.CourseTime, ViewModels.UserIndexCourseList>();
        Mapper.CreateMap<Models.Student, ViewModels.UserIndexCourseList>();

//from the viewmodel
        Mapper.CreateMap<ViewModels.UserIndexCourseList, Models.Course>();
        Mapper.CreateMap<ViewModels.UserIndexCourseList, Models.CourseTime>();
        Mapper.CreateMap<ViewModels.UserIndexCourseList, Models.Student>();

So essentially how can I create a Map which will also pull all of that information so I can use it with my ViewModel that was posted above ? 因此,从本质上讲,我如何创建一个地图,该地图也将提取所有这些信息,以便可以与上面发布的ViewModel一起使用? I have tried numerous options but no luck. 我尝试了很多选择,但没有运气。

I apologize for a similar post I made ahead of time but I don't think I explained myself well enough the first time. 我为我提前发表的类似帖子表示歉意,但我认为我第一次没有对自己做足够的解释。 Thanks again! 再次感谢!

By convention automapper maps properties with same names, so in your case you can do this: 按照惯例,自动映射器以相同的名称映射属性,因此您可以这样做:

public class UserIndexCourseList
{
    ...
    //rename field so it has same name as reference
    public List<CourseTime> CourseTimes{ get; set; }  
}

or you can rename reference in EF so it's name is courseTimeslot. 或者,您可以在EF中重命名引用,因此其名称为courseTimeslot。

Another solution if you don't want to rename your property is to add options to map, for example: 如果您不想重命名属性,另一种解决方案是添加要映射的选项,例如:

Mapper.CreateMap<Models.Course, ViewModels.UserIndexCourseList>()
.ForMember(d => d.courseTimeSlot,
    opt => opt.MapFrom(src => src.CourseTime));

Edit: also they have great documentation, your case is described here: https://github.com/AutoMapper/AutoMapper/wiki/Projection 编辑:他们也有很好的文档,您的情况在这里描述: https : //github.com/AutoMapper/AutoMapper/wiki/Projection

"Because the names of the destination properties do not exactly match up to the source property (CalendarEvent.Date would need to be CalendarEventForm.EventDate), we need to specify custom member mappings in our type map configuration..." “由于目标属性的名称与源属性不完全匹配(CalendarEvent.Date必须为CalendarEventForm.EventDate),因此我们需要在类型映射配置中指定自定义成员映射。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM