Async Indexer in C #

Recently, we switched to EF 6, and we started using asynchronous EF commands. For example, in my repository, I have the following method:

// Gets entities asynchron in a range starting from skip.
// Take defines the maximum number of entities to be returned.
public async Task<IEnumerable<TEntity>> GetRangeAsync(int skip, int take)
{
  var entities = this.AddIncludes(this.DbContext.Set<TEntity>())
        .OrderBy(this.SortSpec.PropertyName)
        .Skip(skip)
        .Take(take)
        .ToListAsync();

  return await entities;
}

Now I need to change the user interface to receive async data. Below is my user interface class; This class is associated with WPF.

public sealed class RepositoryCollectionView<TEntity, TEntityViewModel> : IList<TEntityViewModel>,
                                                                          ICollectionView,
                                                                          IEditableCollectionView,
                                                                          IComparer
...                                                       
public TEntityViewModel this[int index]
{
 get
  {
       return await this.GetItem(index).Result;
  }
  set
  {
    throw new NotSupportedException();
  }
}
...
...
...

Problem: in the user interface, I created a new method called GetItemAsync (index), and I need to call this method from the Indexer; When I write the async keyword for the indexer as follows: public async TEntityViewModel this[int index]I get the following error : "async modifier" is not valid for this element "

Any idea? Any help would be greatly appreciated!

+4
2

async. 10.15 # 5:

async async.

async ( 10.6), (10.9).

, async void, Task Task<T> - , Task<T> Task, ? ( , ?)

, , GetItem - GetItemAsync, Task<TEntityViewModel> - , .

+5

.

, , (no async no await).

+1

Source: https://habr.com/ru/post/1530029/


All Articles