多分组 linq 嵌套 DTO 翻译不好

multiple grouping linq nested DTO not translating well

这是一个 .NET Core Web API 任务方法。我有一个平面 table,我需要将其转换为嵌套 DTO。第一个 DTO 有效,但我似乎无法在分组后嵌套第二个 DTO。

我知道我已经正确地完成了分组。我只是不确定 DTO 的二级嵌套是否正确完成,它抱怨无法转换为某种类型。

有人能指出我正确的方向吗?

 public async Task<List<PointCardViewModel>> GetPointCards()        {
        var data = (from s in db.Students
                    join dc in db.DailyCards on s.StudentId equals dc.StudentId
                    join dcli in db.DailyCardLineItems on dc.CardId equals dcli.CardId
                    join dcob in db.DailyCardOtherBehaviors on dc.CardId equals dcob.CardId
                    select new
                    {
                        s.StudentName,
                        s.StudentGrade,
                        dc.CardId,
                        dc.CardDate,
                        dcli.ClassParticipationPoints,
                        dcli.AssignmentCompletionPoints,
                        dcli.BonusHomeworkPoints,
                        dcli.ClassPeriod,
                        dcob.PersonalAppearancePoints,
                        dcob.LunchPoints,
                        dcob.RecessOtherPoints,
                        dcob.AmHomeroomPoints,
                        dcob.PmHomeroomPoints
                    });

        var queryPointCards = (data
                                 .GroupBy(x => new
                                 {
                                     x.CardId,
                                     x.StudentGrade,
                                     x.StudentName,
                                     x.CardDate,
                                     x.PersonalAppearancePoints,
                                     x.LunchPoints,
                                     x.RecessOtherPoints,
                                     x.AmHomeroomPoints,
                                     x.PmHomeroomPoints
                                 })
                                .Select(x => new PointCardViewModel()
                                {
                                    CardId = x.Key.CardId,
                                    StudentName = x.Key.StudentName,
                                    Grade = x.Key.StudentGrade,
                                    EvaluationDate = x.Key.CardDate,
                                    PersonalAppearancePoints = x.Key.PersonalAppearancePoints,
                                    LunchPoints = x.Key.LunchPoints,
                                    RecessOtherPoints = x.Key.RecessOtherPoints,
                                    AMHomeRoomPoints = x.Key.AmHomeroomPoints,
                                    PMHomeRoomPoints = x.Key.PmHomeroomPoints,

                                    //LineItems = null  --> This works!! But not the below
                                    LineItems = x.Select(c => new LineItemViewModel
                                    {
                                        ClassPeriod = c.ClassPeriod,
                                        BonusHomeworkPoints = c.BonusHomeworkPoints,
                                        ClassParticipationPoints = c.ClassParticipationPoints,
                                        AssignmentCompletionPoints = c.AssignmentCompletionPoints
                                    })
                                }
                              )
                          ).ToListAsync();

        if (db != null)
        {
            return await queryPointCards;
        }
        return null;
    }

您已达到分组限制。摸索后,您无法访问组项目。仅允许来自 'Key' 和聚合函数的字段。

所以只需输入 data.AsEnumerable() 并在客户端进行分组。