EF Core 3.0 Select 索引超载的投影(又名 .Select((entity, index) => new {})失败

EF Core 3.0 Select Projection with index overload (aka .Select((entity, index) => new {}) fails

我有当前设置,带有 Select 索引器投影(实体,索引)(参见 SubRubrics)。如果我将索引器排除在外,问题就解决了......但是,如果我将 SubRubricItems 排除在外,那么我可以使用索引器。它只能在最后一个 select 投影上使用吗,还是..?

下面是 linq 投影、错误消息和更多信息。

await _db
                        .Exams
                        .AsNoTracking()
                        .Include(exam => exam.Stations)
                        .ThenInclude(station => station.Rubrics)
                        .ThenInclude(rubric => rubric.SubRubrics)
                        .ThenInclude(subRubric => subRubric.Items)
                        .Select(exam => new Result.ExamViewModel
                        {
                            Id = exam.Id,
                            Name = exam.Name,
                            Stations = exam.Stations.Select(station => new Result.StationViewModel
                            {
                                Id = station.Id,
                                Description = station.Description,
                                Rubrics = station.Rubrics.Select(rubric => new Result.RubricViewModel
                                {
                                    Id = rubric.Id,
                                    Name = rubric.Name,
                                    Info = rubric.Info,
                                    SubRubrics = rubric.SubRubrics.Select((subRubric, index) => new Result.SubRubricViewModel
                                    {
                                        Id = subRubric.Id,
                                        Order = index,
                                        Name = subRubric.Name,
                                        Info = subRubric.Info,
                                        Type = subRubric.Type.ToString(),
                                        Items = subRubric.Items.Select(item => new Result.SubRubricItemViewModel
                                        {
                                            Id = item.Id,
                                            Name = item.Name
                                        })
                                    })
                                })
                            })
                        })
                        .ToListAsync()

这提供了这个我不明白的错误:/

InvalidOperationException: Processing of the LINQ expression '(MaterializeCollectionNavigation(
    navigation: Navigation: Rubric.SubRubrics,
    subquery: (NavigationExpansionExpression
        Source: DbSet<SubRubric>
            .Where(s0 => !(s0.IsDeleted))
            .Where(s0 => EF.Property<Nullable<long>>(r, "Id") != null && EF.Property<Nullable<long>>(r, "Id") == EF.Property<Nullable<long>>(s0, "RubricId"))
        PendingSelector: s0 => (NavigationTreeExpression
            Value: (EntityReference: SubRubric | IncludePaths: Items)
            Expression: s0)
    )
        .Where(i => EF.Property<Nullable<long>>((NavigationTreeExpression
            Value: (EntityReference: Rubric | IncludePaths: Version SubRubrics->...)
            Expression: r), "Id") != null && EF.Property<Nullable<long>>((NavigationTreeExpression
            Value: (EntityReference: Rubric | IncludePaths: Version SubRubrics->...)
            Expression: r), "Id") == EF.Property<Nullable<long>>(i, "RubricId")))
    .AsQueryable()
    .Select((subRubric, index) => new SubRubricViewModel{ 
        Id = subRubric.Id, 
        Order = index, 
        Name = subRubric.Name, 
        Info = subRubric.Info, 
        Type = subRubric.Type.ToString(), 
        Items = subRubric.Items
            .AsQueryable()
            .Select(item => new SubRubricItemViewModel{ 
                Id = item.Id, 
                Name = item.Name 
            }
            ) 
    }
    )' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.

这曾经有效,直到我为 Items 模型添加了额外的 SubRubricItems select,又名

Items = subRubric.Items.Select(item => new Result.SubRubricItemViewModel
                                        {
                                            Id = item.Id,
                                            Name = item.Name
                                        })

为了便于参考,这是投影到的视图模型:

public sealed class Result
        {
            public IEnumerable<ExamViewModel> Exams { get; set; }

            public sealed class ExamViewModel
            {
                public long Id { get; set; }
                public string Name { get; set; }
                public IEnumerable<StationViewModel> Stations { get; set; }
            }

            public sealed class StationViewModel
            {
                public long Id { get; set; }
                public string Description { get; set; }
                public IEnumerable<RubricViewModel> Rubrics { get; set; }
            }

            public sealed class RubricViewModel
            {
                public long Id { get; set; }
                public string Name { get; set; }
                public string Info { get; set; }
                public IEnumerable<SubRubricViewModel> SubRubrics { get; set; }
            }

            public sealed class SubRubricViewModel
            {
                public long Id { get; set; }
                public int Order { get; set; }
                public string Name { get; set; }
                public string Info { get; set; }
                public string Type { get; set; }
                public IEnumerable<SubRubricItemViewModel> Items { get; set; }
            }

            public sealed class SubRubricItemViewModel
            {
                public long Id { get; set; }
                public int Order { get; set; }
                public string Name { get; set; }
                public string Info { get; set; }
                public string Type { get; set; }
            }
        }

无法翻译成 SQL。因此,运行 SQL 查询在 .Select()

之前
.ThenInclude(subRubric => subRubric.Items)
.AsEnumerable()
.Select(exam => new Result.ExamViewModel

或删除 Includes(当您有自定义投影时它们不执行任何操作,从而更改查询)

SubRubrics = rubric.SubRubrics.Select((subRubric) => new Result.SubRubricViewModel
{
    Id = subRubric.Id,
    Order = 0, . . .

然后在视图模型上填写Order 属性。