mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 23:13:42 +08:00
Synchronization code
This commit is contained in:
parent
e35b565ee6
commit
da406a64bb
@ -1718,6 +1718,10 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader) as List<TResult>;
|
result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader) as List<TResult>;
|
||||||
}
|
}
|
||||||
|
else if (entityType.Name?.StartsWith("ValueTuple`")==true)
|
||||||
|
{
|
||||||
|
result = Db.Context.Utilities.DataReaderToValueTupleType<TResult>(dataReader);
|
||||||
|
}
|
||||||
else if (entityType == UtilConstants.ObjType)
|
else if (entityType == UtilConstants.ObjType)
|
||||||
{
|
{
|
||||||
result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader).Select(it => ((TResult)(object)it)).ToList();
|
result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader).Select(it => ((TResult)(object)it)).ToList();
|
||||||
@ -1750,6 +1754,10 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
result = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader) as List<TResult>;
|
result = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader) as List<TResult>;
|
||||||
}
|
}
|
||||||
|
else if (entityType.Name?.StartsWith("ValueTuple`") == true)
|
||||||
|
{
|
||||||
|
result =await Db.Context.Utilities.DataReaderToValueTupleTypeAsync<TResult>(dataReader);
|
||||||
|
}
|
||||||
else if (entityType == UtilConstants.ObjType)
|
else if (entityType == UtilConstants.ObjType)
|
||||||
{
|
{
|
||||||
var expObj = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader);
|
var expObj = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader);
|
||||||
|
@ -4,6 +4,7 @@ using System.Collections;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
|
using System.DirectoryServices.ActiveDirectory;
|
||||||
using System.Dynamic;
|
using System.Dynamic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
@ -19,6 +20,77 @@ namespace SqlSugar
|
|||||||
public QueryBuilder QueryBuilder { get; set; }
|
public QueryBuilder QueryBuilder { get; set; }
|
||||||
|
|
||||||
#region DataReader
|
#region DataReader
|
||||||
|
public List<T> DataReaderToValueTupleType<T>(IDataReader reader)
|
||||||
|
{
|
||||||
|
var result = new List<T>();
|
||||||
|
|
||||||
|
// Get the property names and types dynamically
|
||||||
|
var propertyNames = Enumerable.Range(0, reader.FieldCount)
|
||||||
|
.Select(reader.GetName).ToList();
|
||||||
|
var propertyTypes = Enumerable.Range(0, reader.FieldCount)
|
||||||
|
.Select(reader.GetFieldType).ToList();
|
||||||
|
|
||||||
|
using (reader)
|
||||||
|
{
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
// Create a new instance of the tuple type using the property types
|
||||||
|
var tupleType = typeof(T);
|
||||||
|
var tupleInstance = Activator.CreateInstance(tupleType);
|
||||||
|
|
||||||
|
// Set the property values dynamically
|
||||||
|
for (int i = 0; i < reader.FieldCount; i++)
|
||||||
|
{
|
||||||
|
var propertyName = propertyNames[i];
|
||||||
|
var propertyValue = reader.GetValue(i);
|
||||||
|
var propertyType = propertyTypes[i];
|
||||||
|
|
||||||
|
var propertyInfo = tupleType.GetFields()[i];
|
||||||
|
propertyInfo.SetValue(tupleInstance, UtilMethods.ChangeType2(propertyValue, propertyType));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the tuple instance to the result list
|
||||||
|
result.Add((T)tupleInstance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public async Task<List<T>> DataReaderToValueTupleTypeAsync<T>(IDataReader reader)
|
||||||
|
{
|
||||||
|
var result = new List<T>();
|
||||||
|
|
||||||
|
// Get the property names and types dynamically
|
||||||
|
var propertyNames = Enumerable.Range(0, reader.FieldCount)
|
||||||
|
.Select(reader.GetName).ToList();
|
||||||
|
var propertyTypes = Enumerable.Range(0, reader.FieldCount)
|
||||||
|
.Select(reader.GetFieldType).ToList();
|
||||||
|
|
||||||
|
using (reader)
|
||||||
|
{
|
||||||
|
while ( await ((DbDataReader)reader).ReadAsync())
|
||||||
|
{
|
||||||
|
// Create a new instance of the tuple type using the property types
|
||||||
|
var tupleType = typeof(T);
|
||||||
|
var tupleInstance = Activator.CreateInstance(tupleType);
|
||||||
|
|
||||||
|
// Set the property values dynamically
|
||||||
|
for (int i = 0; i < reader.FieldCount; i++)
|
||||||
|
{
|
||||||
|
var propertyName = propertyNames[i];
|
||||||
|
var propertyValue = reader.GetValue(i);
|
||||||
|
var propertyType = propertyTypes[i];
|
||||||
|
|
||||||
|
var propertyInfo = tupleType.GetFields()[i];
|
||||||
|
propertyInfo.SetValue(tupleInstance, Convert.ChangeType(propertyValue, propertyType));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the tuple instance to the result list
|
||||||
|
result.Add((T)tupleInstance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///DataReader to Dynamic
|
///DataReader to Dynamic
|
||||||
@ -612,7 +684,8 @@ namespace SqlSugar
|
|||||||
if (prop.PropertyType == UtilConstants.IntType)
|
if (prop.PropertyType == UtilConstants.IntType)
|
||||||
{
|
{
|
||||||
addItem = addItem.ObjToInt();
|
addItem = addItem.ObjToInt();
|
||||||
} else if (UtilMethods.GetUnderType(prop.PropertyType) == UtilConstants.IntType && addItem != null)
|
}
|
||||||
|
else if (UtilMethods.GetUnderType(prop.PropertyType) == UtilConstants.IntType && addItem != null)
|
||||||
{
|
{
|
||||||
addItem = addItem.ObjToInt();
|
addItem = addItem.ObjToInt();
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,8 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
SqlSugarProvider Context { get; set; }
|
SqlSugarProvider Context { get; set; }
|
||||||
QueryBuilder QueryBuilder { get; set; }
|
QueryBuilder QueryBuilder { get; set; }
|
||||||
|
List<T> DataReaderToValueTupleType<T>(IDataReader reader);
|
||||||
|
Task<List<T>> DataReaderToValueTupleTypeAsync<T>(IDataReader reader);
|
||||||
ExpandoObject DataReaderToExpandoObject(IDataReader reader);
|
ExpandoObject DataReaderToExpandoObject(IDataReader reader);
|
||||||
List<ExpandoObject> DataReaderToExpandoObjectList(IDataReader reader);
|
List<ExpandoObject> DataReaderToExpandoObjectList(IDataReader reader);
|
||||||
Task<List<ExpandoObject>> DataReaderToExpandoObjectListAsync(IDataReader dataReader);
|
Task<List<ExpandoObject>> DataReaderToExpandoObjectListAsync(IDataReader dataReader);
|
||||||
|
Loading…
Reference in New Issue
Block a user