Add Queryable ForeachDataReader

This commit is contained in:
sunkaixuan 2024-03-27 11:55:05 +08:00
parent 2114e194cd
commit e1938f0c69
2 changed files with 57 additions and 0 deletions

View File

@ -641,7 +641,62 @@ namespace SqlSugar
var newResult = fieldsHelper.GetSetList(obj, listObj, mappings).Select(it => (T)it).ToList(); var newResult = fieldsHelper.GetSetList(obj, listObj, mappings).Select(it => (T)it).ToList();
return newResult; return newResult;
} }
public void ForEachDataReader(Action<T> action)
{
var queryable = this.Clone();
var sql = queryable.ToSql();
var dr = this.Context.Ado.GetDataReader(sql.Key,sql.Value);
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>();
var columns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList();
var cacheKey = "ForEachDataReader"+typeof(T).GetHashCode()+string.Join(",", columns.Select(it => it.PropertyName));
IDataReaderEntityBuilder<T> entytyList = this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate("cacheKey", () =>
{
var cacheResult = new IDataReaderEntityBuilder<T>(this.Context, dr,
columns.Select(it=>it.DbColumnName).ToList()).CreateBuilder(typeof(T));
return cacheResult;
});
using (dr)
{
while (dr.Read())
{
var order = entytyList.Build(dr);
action(order);
}
}
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection)
{
this.Context.Ado.Close();
}
}
public async Task ForEachDataReaderAsync(Action<T> action)
{
var queryable = this.Clone();
var sql = queryable.ToSql();
var dr =await this.Context.Ado.GetDataReaderAsync(sql.Key, sql.Value);
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>();
var columns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList();
var cacheKey = "ForEachDataReader" + typeof(T).GetHashCode() + string.Join(",", columns.Select(it => it.PropertyName));
IDataReaderEntityBuilder<T> entytyList = this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate("cacheKey", () =>
{
var cacheResult = new IDataReaderEntityBuilder<T>(this.Context, dr,
columns.Select(it => it.DbColumnName).ToList()).CreateBuilder(typeof(T));
return cacheResult;
});
using (dr)
{
while (dr.Read())
{
var order = entytyList.Build(dr);
action(order);
}
}
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection)
{
this.Context.Ado.Close();
}
}
public virtual void ForEach(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null) public virtual void ForEach(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null)
{ {
Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0, ErrorMessage.GetThrowMessage("no support Skip take, use PageForEach", "不支持Skip Take,请使用 Queryale.PageForEach")); Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0, ErrorMessage.GetThrowMessage("no support Skip take, use PageForEach", "不支持Skip Take,请使用 Queryale.PageForEach"));

View File

@ -165,6 +165,8 @@ namespace SqlSugar
ISugarQueryable<T> Select(string select); ISugarQueryable<T> Select(string select);
ISugarQueryable<TResult> SelectMergeTable<TResult>(Expression<Func<T, TResult>> expression); ISugarQueryable<TResult> SelectMergeTable<TResult>(Expression<Func<T, TResult>> expression);
ISugarQueryable<T> MergeTable(); ISugarQueryable<T> MergeTable();
void ForEachDataReader(Action<T> action);
Task ForEachDataReaderAsync(Action<T> action);
void ForEach(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); void ForEach(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null);
Task ForEachAsync(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); Task ForEachAsync(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null);
void ForEachByPage(Action<T> action, int pageIndex, int pageSize, ref int totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); void ForEachByPage(Action<T> action, int pageIndex, int pageSize, ref int totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null);