首页 > 解决方案 > SelectMany 不适用于 cosmosDb 的子属性?

问题描述

当我尝试在针对 cosmosdb 构建的可查询对象上使用 selectMany 时,我总是遇到异常:

LINQ 表达式 'DbSet .Where(t => t.Id == __timelineId_0) .SelectMany( 来源: t => EF.Property>(t, "GraduationEvents") .AsQueryable(), collectionSelector: (t, c) = > new TransparentIdentifier(Outer = t, Inner = c))' 无法翻译。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

查询无关紧要,总是在我使用 selectMany 时出现此错误。

示例查询:

 await _dbContext.Timelines.Where(x => x.Id == timelineId).Select(x => x.GraduationEvents).SelectMany(x => x).ToListAsync();

我的实体配置:

        public void Configure(EntityTypeBuilder<Timeline> builder)
    {
        builder.HasKey(x => x.Id);
        builder.HasAlternateKey(x => x.Id);
        builder.OwnsMany(x => x.GraduationEvents, x => x.OwnsMany(graduationEvent => graduationEvent.Subjects));
    }

我也尝试使用本机 cosmosClient,但是当我使用普通 sql 查询基础时,我得到空对象(全为空)。任何想法我做错了什么?

Sajid - 我尝试了你的解决方案,但例外仍然相同

标签: .net-coreentity-framework-coreazure-cosmosdbentity-framework-core-cosmosdb

解决方案


尝试通过 List 属性 (GraduationEvents) 直接调用 .SelectMany()。

然后我通常调用 AsDocumentQuery() 生成对 CosmosDB 的查询,然后执行该查询以检索结果。

一些伪 c# 来澄清这一点:

var query = this.documentClient.CreateDocumentQuery(uri, options)
    .SelectMany(x => x.GraduationEvents).AsDocumentQuery();

List<T> results = new List<T>();
while (query.HasMoreResults)
{
    results.AddRange(await query.ExecuteNextAsync());
}

编辑:此方法使用本机 Azure DocumentClient 库。


推荐阅读