<tfoot id='g6tli'></tfoot>

      <small id='g6tli'></small><noframes id='g6tli'>

      <legend id='g6tli'><style id='g6tli'><dir id='g6tli'><q id='g6tli'></q></dir></style></legend>
      <i id='g6tli'><tr id='g6tli'><dt id='g6tli'><q id='g6tli'><span id='g6tli'><b id='g6tli'><form id='g6tli'><ins id='g6tli'></ins><ul id='g6tli'></ul><sub id='g6tli'></sub></form><legend id='g6tli'></legend><bdo id='g6tli'><pre id='g6tli'><center id='g6tli'></center></pre></bdo></b><th id='g6tli'></th></span></q></dt></tr></i><div id='g6tli'><tfoot id='g6tli'></tfoot><dl id='g6tli'><fieldset id='g6tli'></fieldset></dl></div>
      • <bdo id='g6tli'></bdo><ul id='g6tli'></ul>
    1. ToArrayAsync() 抛出“源 IQueryable 未实现 IAsyncEnumerable"

      时间:2023-06-08

            <tbody id='OysoC'></tbody>

          <small id='OysoC'></small><noframes id='OysoC'>

          • <bdo id='OysoC'></bdo><ul id='OysoC'></ul>
            <tfoot id='OysoC'></tfoot>
                <i id='OysoC'><tr id='OysoC'><dt id='OysoC'><q id='OysoC'><span id='OysoC'><b id='OysoC'><form id='OysoC'><ins id='OysoC'></ins><ul id='OysoC'></ul><sub id='OysoC'></sub></form><legend id='OysoC'></legend><bdo id='OysoC'><pre id='OysoC'><center id='OysoC'></center></pre></bdo></b><th id='OysoC'></th></span></q></dt></tr></i><div id='OysoC'><tfoot id='OysoC'></tfoot><dl id='OysoC'><fieldset id='OysoC'></fieldset></dl></div>

              1. <legend id='OysoC'><style id='OysoC'><dir id='OysoC'><q id='OysoC'></q></dir></style></legend>
                本文介绍了ToArrayAsync() 抛出“源 IQueryable 未实现 IAsyncEnumerable"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 ASP.NET Core 上有一个 MVC 项目,我的问题与 IQueryable 和异步有关.我在 IQueryable<T> 中编写了以下搜索方法:

                I have a MVC project on ASP.NET Core, my problem is connected with IQueryable and asynchronous. I wrote the following method for search in IQueryable<T>:

                private IQueryable<InternalOrderInfo> WhereSearchTokens(IQueryable<InternalOrderInfo> query, SearchToken[] searchTokens)
                {
                    if (searchTokens.Length == 0)
                    {
                        return query;
                    }
                    var results = new List<InternalOrderInfo>();
                    foreach (var searchToken in searchTokens)
                    {
                        //search logic, intermediate results are being added to `results` using `AddRange()`
                    }
                
                    return results.Count != 0 ? results.Distinct().AsQueryable() : query;
                }
                

                我在方法 ExecuteAsync() 中调用它:

                I call this in method ExecuteAsync():

                public async Task<GetAllInternalOrderInfoResponse> ExecuteAsync(GetAllInternalOrderInfoRequest request)
                {
                    //rest of the code
                    if (searchTokens != null && searchTokens.Any())
                    {
                        allInternalOrderInfo = WhereSearchTokens(allInternalOrderInfo, searchTokens);
                    }
                    var orders = await allInternalOrderInfo.Skip(offset).Take(limit).ToArrayAsync();
                    //rest of the code
                }
                

                当我测试这个时,我在调用 ToArrayAsync()

                When I test this I get an InvalidOperationException on line where I call ToArrayAsync()

                源 IQueryable 未实现 IAsyncEnumerable.只有实现 IAsyncEnumerable 的源才能用于实体框架异步操作.

                The source IQueryable doesn't implement IAsyncEnumerable. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.

                我已将 ToArrayAsync() 更改为 ToListAsync() 但没有任何改变.我已经搜索了这个问题一段时间,但解决的问题主要与 DbContext 和实体创建有关.该项目没有安装EntityFramework,由于应用程序架构,最好不要安装.希望有人对我的情况有什么想法.

                I had changed ToArrayAsync() to ToListAsync() but nothing have changed. I have searched this problem for a while, but resolved questions are connected mostly with DbContext and entity creating. EntityFramework is not installed for this project and it's better not to do it because of application architecture. Hope someone has any ideas what to do in my situation.

                推荐答案

                如果你不打算改变你的设计 - 你有几个选择:

                If you are not going to change your design - you have several options:

                1) 将 AsQueryable 更改为另一个返回 IQueryable 的方法,该方法也实现了 IDbAsyncEnumerable.例如,您可以扩展 EnumerableQuery(由 AsQueryable 返回):

                1) Change AsQueryable to another method which returns IQueryable which also implements IDbAsyncEnumerable. For example you can extend EnumerableQuery (which is returned by AsQueryable):

                public class AsyncEnumerableQuery<T> : EnumerableQuery<T>, IDbAsyncEnumerable<T> {
                    public AsyncEnumerableQuery(IEnumerable<T> enumerable) : base(enumerable) {
                    }
                
                    public AsyncEnumerableQuery(Expression expression) : base(expression) {
                    }
                
                    public IDbAsyncEnumerator<T> GetAsyncEnumerator() {
                        return new InMemoryDbAsyncEnumerator<T>(((IEnumerable<T>) this).GetEnumerator());
                    }
                
                    IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator() {
                        return GetAsyncEnumerator();
                    }
                
                    private class InMemoryDbAsyncEnumerator<T> : IDbAsyncEnumerator<T> {
                        private readonly IEnumerator<T> _enumerator;
                
                        public InMemoryDbAsyncEnumerator(IEnumerator<T> enumerator) {
                            _enumerator = enumerator;
                        }
                
                        public void Dispose() {
                        }
                
                        public Task<bool> MoveNextAsync(CancellationToken cancellationToken) {
                            return Task.FromResult(_enumerator.MoveNext());
                        }
                
                        public T Current => _enumerator.Current;
                
                        object IDbAsyncEnumerator.Current => Current;
                    }
                }
                

                然后你改变

                results.Distinct().AsQueryable()
                

                new AsyncEnumerableQuery<InternalOrderInfo>(results.Distinct())
                

                以后,ToArrayAsync 将不再抛出异常(显然你可以创建自己的扩展方法,如 AsQueryable).

                And later, ToArrayAsync will not throw exception any more (obviously you can create your own extension method like AsQueryable).

                2) 更改 ToArrayAsync 部分:

                public static class EfExtensions {
                    public static Task<TSource[]> ToArrayAsyncSafe<TSource>(this IQueryable<TSource> source) {
                        if (source == null)
                            throw new ArgumentNullException(nameof(source));
                        if (!(source is IDbAsyncEnumerable<TSource>))
                            return Task.FromResult(source.ToArray());
                        return source.ToArrayAsync();
                    }
                }
                

                并使用 ToArrayAsyncSafe 而不是 ToArrayAsync,如果 IQueryable 不是 IDbAsyncEnumerable,它将回退到同步枚举.在您的情况下,这只发生在查询实际上是内存列表而不是查询时,因此异步执行无论如何都没有意义.

                And use ToArrayAsyncSafe instead of ToArrayAsync, which will fallback to synchronous enumeration in case IQueryable is not IDbAsyncEnumerable. In your case this only happens when query is really in-memory list and not query, so async execution does not make sense anyway.

                这篇关于ToArrayAsync() 抛出“源 IQueryable 未实现 IAsyncEnumerable"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:无法使用 UrlHelper 下一篇:如何在 ASP.NET Core 中强制执行小写路由?

                相关文章

              2. <tfoot id='CW8xC'></tfoot>

              3. <legend id='CW8xC'><style id='CW8xC'><dir id='CW8xC'><q id='CW8xC'></q></dir></style></legend>

                  1. <i id='CW8xC'><tr id='CW8xC'><dt id='CW8xC'><q id='CW8xC'><span id='CW8xC'><b id='CW8xC'><form id='CW8xC'><ins id='CW8xC'></ins><ul id='CW8xC'></ul><sub id='CW8xC'></sub></form><legend id='CW8xC'></legend><bdo id='CW8xC'><pre id='CW8xC'><center id='CW8xC'></center></pre></bdo></b><th id='CW8xC'></th></span></q></dt></tr></i><div id='CW8xC'><tfoot id='CW8xC'></tfoot><dl id='CW8xC'><fieldset id='CW8xC'></fieldset></dl></div>

                      <bdo id='CW8xC'></bdo><ul id='CW8xC'></ul>

                    <small id='CW8xC'></small><noframes id='CW8xC'>