using System.Collections; using System.Linq.Expressions; namespace Connected.Expressions.Query; internal sealed class EntityQuery : IQueryable, IAsyncEnumerable, IOrderedQueryable { public EntityQuery(IQueryProvider provider, Expression expression) { Provider = provider; Expression = expression; } public Type ElementType => typeof(TEntity); public Expression Expression { get; } public IQueryProvider Provider { get; } public async IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { var result = Provider.Execute(Expression); if (result is IEnumerable en) { var enumerator = en.GetEnumerator(); while (enumerator.MoveNext()) { await Task.CompletedTask; yield return (TEntity)enumerator.Current; } } } public IEnumerator GetEnumerator() { return ((IEnumerable)Provider.Execute(Expression)).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)Provider.Execute(Expression)).GetEnumerator(); } }