DataObjects-NET / dataobjects-net

https://dataobjects.net
MIT License
61 stars 25 forks source link

EntitySet and ToListAsync extension method runtime error #394

Open axmty opened 2 months ago

axmty commented 2 months ago

Hi,

In my company we are migrating to DataObjects v7, but we encountered some problems concerning the AsAsync extension method that seemed to disappear, and that we used everywhere (on IQueryable or EntitySet objects). It seems that we can use ToListAsync instead, but we have a problem when using it on EntitySet.

When reading the code of EntitySet, we expected ToListAsync to work since EntitySet implements IQueryable, but there is a runtime error because EntitySet does not implement IAsyncEnumerable. Is there a reason why EntitySet does not implement IAsyncEnumerable?

After reading the implementation of Queryable.GetAsyncEnumerator, I think we could do the same in the EntitySet class (that is using EntitySet.Provider to get the same result):

public async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
  var result = await Provider.ExecuteSequenceAsync<T>(expression, cancellationToken).ConfigureAwait(false);
  var asyncSource = result.AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false);
  await foreach (var element in asyncSource) {
    yield return element;
  }
}

But maybe we are missing something that prevents us to do so.

We found a small workaround (myEntitySet.Where(x => 1 == 1).ToListAsync()) for the moment, but I would greatly appreciate some explanation.

Thanks a lot, Alex

axmty commented 2 months ago

I have opened a PR here : #395

Let me know if that is ok.