dotnetcore / dotnetcore/FreeSql

添加ToAsyncEnumerable返回异步流功能

Open
#1,982 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

#### Feature 特性

```c#
public class FreeSqlAsyncEnumerable(DbDataReader reader) : IAsyncEnumerable
{
public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken())
{
return new FreeSqlAsyncEnumerator(reader, cancellationToken);
}
}

public class FreeSqlAsyncEnumerator(DbDataReader reader, CancellationToken cancellationToken) : IAsyncEnumerator
{
public T Current => (T) Utils.ExecuteArrayRowReadClassOrTuple(flag, typeof(T), indexes, reader, 0, _util).Value;

public ValueTask MoveNextAsync()
{
return new ValueTask(reader.ReadAsync(cancellationToken));
}

public async ValueTask DisposeAsync()
{
await reader.CloseAsync();
await reader.DisposeAsync();
}
}
```

#### 简要描述原因

针对大数据导出和后续处理非常有用。ToChunk和ToChunkAsync只支持传递委托进去,使得代码耦合。而且没有数据返回。
返回IAsyncEnumberable接口,可以把查询和后续数据处理分离出来,代码不耦合。而且还可以返回数据。其次避免ToListAsync中List扩容的数据复制带来的开销。

#### 使用场景

```c#
freeSql.Select.ToAsyncEnumerable(cancellationToken);
freeSql.Select.ToAsyncEnumerable(t => new T2()
{
Id = t.Id
}, cancellationToken);
freeSql.Select.ToAsyncEnumerable((t1, t2) => new T3()
{
Id = t1.Id,
Name = t2.Name
}, cancellationToken);
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.