ThenInclude方法使用List <element>而不是元素

时间:2017-11-13 15:03:38

标签: c# entity-framework-core

我有以下实体:

public class LlabAppUser
{       
    public int Id { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string PhotoPath { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime EntryDate { get; set; }

    public List<LlabAppUserCourseSubject> LlabAppUserCourseSubjects { get; set; }
}

public class LlabAppUserCourseSubject
{
    public int LlabAppUserId { get; set; }
    public LlabAppUser LlabAppUser { get; set; }
    public int CourseSubjectId { get; set; }
    public CourseSubject CourseSubject { get; set; }
}

public class CourseSubject
{
    public int Id { get; set; }
    public int SubjectId { get; set; }
    public Subject Subject { get; set; }
    public int CourseId { get; set; }
    public Course Course { get; set; }

    public List<Theme> Themes { get; set; }
    public List<LlabAppUserCourseSubject> LlabAppUserCourseSubjects { get; set; }
}

现在要获取用户及其CourseSubjects,我使用以下代码:

var llabAppUser = await _context.LlabAppUsers
    .Include(user => user.LlabAppUserCourseSubjects)
    .ThenInclude(-NOT WORKING Getting List<LlabAppUserCourseSubject> instead of LlabAppUserCourseSubject-)
    .FirstOrDefaultAsync(user => user.Id == userId);

var courseSubjects =  llabAppUser.LlabAppUserCourseSubjects
    .Select(usercs => usercs.CourseSubject).ToList();

因此,“ThenInclude”方法有两个重载:一个获取List<LlabAppUserCourseSubject>作为函数的参数,另一个获得LlabAppUserCourseSubject。但正如官方文档在此处所说:https://docs.microsoft.com/en-us/ef/core/querying/related-data,我应该得到一个LlabAppUserCourseSubject元素,而不是列表。

0 个答案:

没有答案