diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index fa18a615a..313179e7c 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -308,6 +308,8 @@ namespace SqlSugar column.IsIgnore = true; column.NoSerialize = sugarColumn.NoSerialize; column.ColumnDescription = sugarColumn.ColumnDescription; + column.IsJson = sugarColumn.IsJson; + column.IsArray = sugarColumn.IsArray; } } if (column.ColumnDescription.IsNullOrEmpty()) column.ColumnDescription = GetPropertyAnnotation(result.Type, column.PropertyName); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 444c30a19..4da31981e 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1358,19 +1358,45 @@ namespace SqlSugar var list= this.ToPivotList(columnSelector, rowSelector, dataSelector); return this.Context.Utilities.SerializeObject(list); } - public List ToChildList(Expression> parentIdExpression, object primaryKeyValue) + public List ToChildList(Expression> parentIdExpression, object primaryKeyValue,bool isContainOneself = true) { var entity = this.Context.EntityMaintenance.GetEntityInfo(); var pk = GetTreeKey(entity); var list = this.ToList(); - return GetChildList(parentIdExpression, pk, list, primaryKeyValue); + if (isContainOneself) + { + var result= GetChildList(parentIdExpression, pk, list, primaryKeyValue); + var addItem = this.Context.Queryable().In(pk, primaryKeyValue).First(); + if (addItem != null) + { + result.Add(addItem); + } + return result; + } + else + { + return GetChildList(parentIdExpression, pk, list, primaryKeyValue); + } } - public async Task> ToChildListAsync(Expression> parentIdExpression, object primaryKeyValue) + public async Task> ToChildListAsync(Expression> parentIdExpression, object primaryKeyValue,bool isContainOneself=true) { var entity = this.Context.EntityMaintenance.GetEntityInfo(); var pk = GetTreeKey(entity); var list = await this.ToListAsync(); - return GetChildList(parentIdExpression,pk,list, primaryKeyValue); + if (isContainOneself) + { + var result = GetChildList(parentIdExpression, pk, list, primaryKeyValue); + var addItem = this.Context.Queryable().In(pk, primaryKeyValue).First(); + if (addItem != null) + { + result.Add(addItem); + } + return result; + } + else + { + return GetChildList(parentIdExpression, pk, list, primaryKeyValue); + } } public List ToParentList(Expression> parentIdExpression, object primaryKeyValue) { diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubDistinctCount.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubDistinctCount.cs new file mode 100644 index 000000000..959cd89e9 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubDistinctCount.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace SqlSugar +{ + public class SubDistinctCount : ISubOperation + { + public bool HasWhere + { + get; set; + } + + public string Name + { + get + { + return "DistinctCount"; + } + } + + public Expression Expression + { + get; set; + } + + + public int Sort + { + get + { + return 200; + } + } + + public ExpressionContext Context + { + get; set; + } + + public string GetValue(Expression expression = null) + { + var exp = expression as MethodCallExpression; + var argExp = exp.Arguments[0]; + var parametres = (argExp as LambdaExpression).Parameters; + if ((argExp as LambdaExpression).Body is UnaryExpression) + { + argExp = ((argExp as LambdaExpression).Body as UnaryExpression).Operand; + } + var argLambda = argExp as LambdaExpression; + if (this.Context.InitMappingInfo != null && argLambda != null && argLambda.Parameters.Count > 0) + { + foreach (var item in argLambda.Parameters) + { + this.Context.InitMappingInfo(item.Type); + } + this.Context.RefreshMapping(); + } + var result = "COUNT(DISTINCT " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")"; + var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot; + result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context)); + return result; + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs index c3f28d26f..bd001e618 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs @@ -36,7 +36,8 @@ namespace SqlSugar new SubHaving(){ Context=Context}, new SubWithNolock(){ Context=Context }, new SubEnableTableFilter(){ Context=Context }, - new SubSelectStringJoin{ Context=Context } + new SubSelectStringJoin{ Context=Context }, + new SubDistinctCount{ Context=Context } }; } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Subquerable.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Subquerable.cs index e47326180..ae4555b9b 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Subquerable.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Subquerable.cs @@ -120,6 +120,11 @@ namespace SqlSugar { return default(string); } + + public int DistinctCount(Func expression) + { + return default(int); + } public TResult Sum(Func expression) where TResult : struct { return default(TResult); diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs index 4a40f11b0..6a951d035 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs @@ -172,8 +172,8 @@ namespace SqlSugar Task ToJsonPageAsync(int pageIndex, int pageSize, RefAsync totalNumber); KeyValuePair> ToSql(); string ToSqlString(); - List ToChildList(Expression> parentIdExpression, object primaryKeyValue); - Task> ToChildListAsync(Expression> parentIdExpression, object primaryKeyValue); + List ToChildList(Expression> parentIdExpression, object primaryKeyValue, bool isContainOneself = true); + Task> ToChildListAsync(Expression> parentIdExpression, object primaryKeyValue, bool isContainOneself = true); List ToParentList(Expression> parentIdExpression, object primaryKeyValue); Task> ToParentListAsync(Expression> parentIdExpression, object primaryKeyValue); List ToTree(Expression>> childListExpression, Expression> parentIdExpression,object rootValue);