【问题标题】:multiple grouping linq nested DTO not translating well多个分组 linq 嵌套 DTO 不能很好地翻译
【发布时间】:2020-11-15 22:45:17
【问题描述】:

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

我知道我已经正确完成了分组。我只是不确定 DTO 的第二级嵌套是否正确,它抱怨无法转换为某种类型。 LINQ Query to put data in a nested object

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

 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;
    }

【问题讨论】:

    标签: c# linq asp.net-core asp.net-core-webapi dto


    【解决方案1】:

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

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

    【讨论】:

    • 在客户端分组是什么意思?我正在尝试做这样的事情:stackoverflow.com/questions/50973631/…
    • 在 AsEnumerable() 数据被物化并在客户端提供分组之后。链接回答错误,它可能适用于 EF 2.1,但不适用于最新版本。
    • 非常感谢。这对我帮助很大..我很迷茫!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2021-05-19
    • 2014-07-08
    相关资源
    最近更新 更多