无法翻译给定的 'GroupBy' 模式。在 'GroupBy' 之前调用 'AsEnumerable' 以在客户端对其进行评估
Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side
我有一个项目列表“listItems”,就像这样
Id Code Value
1 'a' '1'
2 'a' '2'
3 'b' 'x'
4 'b' 'y'
并获得以下
'a' => '1', '2'
'b' => 'x', 'y'
我通过 EF Core 使用以下代码
public async Task<Dictionary<string, List<string>>> GetAllListsAsync() =>
await _context.ListItems
.GroupBy(x => x.Code)
.OrderBy(x => x.Key)
.ToDictionaryAsync(x => x.Key, x => x.Select(y => y.Value)
.OrderBy(v => v)
.ToList());
我在Swagger的API中得到以下错误:
System.InvalidOperationException: Unable to translate the given
'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it
client-side. at
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(Expression
extensionExpression) at
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(Expression
shaperExpression, RelationalCommandCache& relationalCommandCache,
LambdaExpression& relatedDataLoaders) at
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression
shapedQueryExpression)
我尝试做的事情:
如果我在 GroupBy 之前使用 AsEnumerable,它将不再找到 ToDictionaryAsync:
我测试了在AsEnumlerable
之后添加AsQueryable
编译错误信息在编译时消失了,但在运行时保持不变(无法翻译'GroupBy'模式。在...之前调用 'AsEnumerable')
这个我也试过了
public async Task<Dictionary<string, List<string>>> GetAllListsAsync() =>
await _context.ListItems
.ToLookup(x => x.Code, x => x.Value)
.AsQueryable()
.ToDictionaryAsync(x => x.Key, x => x.Select(y => y)
.ToList());
运行时在那种情况下说:
The source 'IQueryable' doesn't implement
'IAsyncEnumerable<System.Linq.IGrouping2[System.String,System.String]>'. Only sources that implement 'IAsyncEnumerable' can be used for Entity Framework asynchronous operations. at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsAsyncEnumerable[TSource](IQueryable
1
source) at
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToDictionaryAsync[TSource,TKey,TElement](IQueryable1 source, Func
2 keySelector, Func2 elementSelector, IEqualityComparer
1 comparer, CancellationToken cancellationToken)
由于加载了所有记录,因此必须在客户端进行分组。请注意,在具体化对象后,您不能使用异步可查询扩展。
public async Task<Dictionary<string, List<string>>> GetAllListsAsync() =>
(await _context.ListItems.ToListAsync())
.GroupBy(x => x.Code)
.OrderBy(x => x.Key)
.ToDictionary(x => x.Key, x => x.Select(y => y.Value)
.OrderBy(v => v)
.ToList());
我有一个项目列表“listItems”,就像这样
Id Code Value
1 'a' '1'
2 'a' '2'
3 'b' 'x'
4 'b' 'y'
并获得以下
'a' => '1', '2'
'b' => 'x', 'y'
我通过 EF Core 使用以下代码
public async Task<Dictionary<string, List<string>>> GetAllListsAsync() =>
await _context.ListItems
.GroupBy(x => x.Code)
.OrderBy(x => x.Key)
.ToDictionaryAsync(x => x.Key, x => x.Select(y => y.Value)
.OrderBy(v => v)
.ToList());
我在Swagger的API中得到以下错误:
System.InvalidOperationException: Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side. at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(Expression extensionExpression) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression, RelationalCommandCache& relationalCommandCache, LambdaExpression& relatedDataLoaders) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
我尝试做的事情:
如果我在 GroupBy 之前使用 AsEnumerable,它将不再找到 ToDictionaryAsync:
我测试了在
AsEnumlerable
之后添加AsQueryable
编译错误信息在编译时消失了,但在运行时保持不变(无法翻译'GroupBy'模式。在...之前调用 'AsEnumerable')这个我也试过了
public async Task<Dictionary<string, List<string>>> GetAllListsAsync() => await _context.ListItems .ToLookup(x => x.Code, x => x.Value) .AsQueryable() .ToDictionaryAsync(x => x.Key, x => x.Select(y => y) .ToList());
运行时在那种情况下说:
The source 'IQueryable' doesn't implement 'IAsyncEnumerable<System.Linq.IGrouping
2[System.String,System.String]>'. Only sources that implement 'IAsyncEnumerable' can be used for Entity Framework asynchronous operations. at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsAsyncEnumerable[TSource](IQueryable
1 source) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToDictionaryAsync[TSource,TKey,TElement](IQueryable1 source, Func
2 keySelector, Func2 elementSelector, IEqualityComparer
1 comparer, CancellationToken cancellationToken)
由于加载了所有记录,因此必须在客户端进行分组。请注意,在具体化对象后,您不能使用异步可查询扩展。
public async Task<Dictionary<string, List<string>>> GetAllListsAsync() =>
(await _context.ListItems.ToListAsync())
.GroupBy(x => x.Code)
.OrderBy(x => x.Key)
.ToDictionary(x => x.Key, x => x.Select(y => y.Value)
.OrderBy(v => v)
.ToList());