Bug Select(z=>z.Json)

This commit is contained in:
sunkaixuan
2022-09-20 15:59:17 +08:00
parent e1477afbdf
commit 1630483b99
5 changed files with 77 additions and 0 deletions

View File

@@ -2581,6 +2581,7 @@ namespace SqlSugar
result.SqlBuilder = this.SqlBuilder;
result.SqlBuilder.QueryBuilder.Parameters = QueryBuilder.Parameters;
result.SqlBuilder.QueryBuilder.SelectValue = expression;
result.SqlBuilder.QueryBuilder.IsSelectSingleFiledJson = UtilMethods.IsJsonMember(expression,this.Context);
if (this.IsCache)
{
result.WithCache(this.CacheTime);
@@ -3370,6 +3371,10 @@ namespace SqlSugar
{
result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader).Select(it => ((TResult)(object)it)).ToList();
}
else if (QueryBuilder.IsSelectSingleFiledJson)
{
result= this.Context.Utilities.DataReaderToSelectJsonList<TResult>(dataReader);
}
else if (entityType.IsAnonymousType() || isComplexModel)
{
result = this.Context.Utilities.DataReaderToList<TResult>(dataReader);
@@ -3393,6 +3398,10 @@ namespace SqlSugar
var expObj = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader);
result = expObj.Select(it => ((TResult)(object)it)).ToList();
}
else if (QueryBuilder.IsSelectSingleFiledJson)
{
result= await this.Context.Utilities.DataReaderToSelectJsonListAsync<TResult>(dataReader);
}
else if (entityType.IsAnonymousType() || isComplexModel)
{
result =await this.Context.Utilities.DataReaderToListAsync<TResult>(dataReader);

View File

@@ -33,6 +33,7 @@ namespace SqlSugar
#endregion
#region Splicing basic
public bool IsSelectSingleFiledJson { get; set; }
public string TranLock { get; set; }
public bool IsDisableMasterSlaveSeparation { get; set; }
public bool IsEnableMasterSlaveSeparation { get; set; }

View File

@@ -202,6 +202,30 @@ namespace SqlSugar
return reval;
}
}
public List<T> DataReaderToSelectJsonList<T>(IDataReader dataReader)
{
List<T> result = new List<T>();
using (dataReader)
{
while (dataReader.Read())
{
var value = dataReader.GetValue(0);
if (value == null || value == DBNull.Value)
{
result.Add(default(T));
}
else
{
result.Add(Context.Utilities.DeserializeObject<T>(value.ToString()));
}
}
}
return result;
}
/// <summary>
/// DataReaderToList
/// </summary>
@@ -249,6 +273,29 @@ namespace SqlSugar
return reval;
}
}
public async Task<List<T>> DataReaderToSelectJsonListAsync<T>(IDataReader dataReader)
{
List<T> result = new List<T>();
using (dataReader)
{
while (await ((DbDataReader)dataReader).ReadAsync())
{
var value = dataReader.GetValue(0);
if (value == null || value == DBNull.Value)
{
result.Add(default(T));
}
else
{
result.Add(Context.Utilities.DeserializeObject<T>(value.ToString()));
}
}
}
return result;
}
/// <summary>
/// DataReaderToList
/// </summary>

View File

@@ -17,6 +17,8 @@ namespace SqlSugar
List<ExpandoObject> DataReaderToExpandoObjectListNoUsing(IDataReader reader);
Task<List<ExpandoObject>> DataReaderToExpandoObjectListAsyncNoUsing(IDataReader dataReader);
List<T> DataReaderToList<T>(IDataReader reader);
List<T> DataReaderToSelectJsonList<T>(IDataReader reader);
Task<List<T>> DataReaderToSelectJsonListAsync<T>(IDataReader reader);
List<T> DataReaderToListNoUsing<T>(IDataReader reader);
Task<List<T>> DataReaderToListAsync<T>(IDataReader dataReader);
Task<List<T>> DataReaderToListAsyncNoUsing<T>(IDataReader dataReader);

View File

@@ -17,6 +17,24 @@ namespace SqlSugar
{
public class UtilMethods
{
internal static bool IsJsonMember(Expression expression, SqlSugarProvider context)
{
if (expression == null)
return false;
if (!(expression is LambdaExpression))
return false;
var lambda = (LambdaExpression)expression;
if (!(lambda.Body is MemberExpression))
return false;
var member = lambda.Body as MemberExpression;
if (!(member.Type.IsClass()))
return false;
if (member.Expression == null)
return false;
var entity = context.EntityMaintenance.GetEntityInfo(member.Expression.Type);
var json = entity.Columns.FirstOrDefault(z => z.IsJson && z.PropertyName == member.Member.Name);
return json != null;
}
public static string GetSeparatorChar()
{
return Path.Combine("a", "a").Replace("a", "");