From 059ca5650eb2f2f9b20bd42161cdec4a97abebc4 Mon Sep 17 00:00:00 2001 From: skx <610262374@qq.com> Date: Sun, 31 Jan 2021 16:15:53 +0800 Subject: [PATCH] one to many support any(y=>y.id>0) --- .../SqlServerTest/UnitTest/UQueryable2.cs | 5 ++- .../ResolveItems/MapperExpressionResolve.cs | 41 +++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs index bf0c46c78..1afaca31d 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs @@ -32,7 +32,10 @@ namespace OrmTest .Mapper(it => it.Items, it => it.Items.First().OrderId) .Where(it => it.Items.Any()) .ToList(); - + var list7= Db.Queryable() + .Mapper(it => it.Items, it => it.Items.First().OrderId) + .Where(it => it.Items.Any(y => y.ItemId == 1)) + .ToList(); var sql=Db.Queryable().AS("[order]").ToList(); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MapperExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MapperExpressionResolve.cs index 8a1548d95..9487fb7b9 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MapperExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MapperExpressionResolve.cs @@ -45,7 +45,7 @@ namespace SqlSugar //var SelectInfo = GetSelectInfo(expression); this.context.InitMappingInfo(childExpression.Expression.Type); var entity = this.context.EntityMaintenance.GetEntityInfo(childExpression.Expression.Type); - oneToMany(callName, entity, childExpression.Expression.ToString(), fillInfo, mappingFild1Info, mappingFild1Info2); + oneToMany(methodExpression, callName, entity, childExpression.Expression.ToString(), fillInfo, mappingFild1Info, mappingFild1Info2); } private void ResolveMember() @@ -99,33 +99,60 @@ namespace SqlSugar .Select(sqlBuilder.GetTranslationColumnName(selectInfo.FieldName)).ToSql().Key; } - private void oneToMany(string methodName,EntityInfo mainEntity,string shortName,MapperExpressionInfo fillInfo, MapperExpressionInfo mappingFild1Info, MapperExpressionInfo mappingFild1Info2) + private void oneToMany(MethodCallExpression methodCallExpression,string methodName,EntityInfo mainEntity,string shortName,MapperExpressionInfo fillInfo, MapperExpressionInfo mappingFild1Info, MapperExpressionInfo mappingFild1Info2) { - var pkColumn = mainEntity.Columns.FirstOrDefault(it=>it.IsPrimarykey==true); + var pkColumn = mainEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true); if (pkColumn == null) { pkColumn = mainEntity.Columns.FirstOrDefault(); } var tableName = sqlBuilder.GetTranslationTableName(fillInfo.EntityInfo.DbTableName); var whereLeft = sqlBuilder.GetTranslationColumnName(mappingFild1Info.FieldString); - var whereRight = sqlBuilder.GetTranslationColumnName(shortName+"."+pkColumn.DbColumnName); - + var whereRight = sqlBuilder.GetTranslationColumnName(shortName + "." + pkColumn.DbColumnName); + string whereExpression = GetWhereExpression(methodCallExpression); if (methodName == "Any") { - this.sql = " ("+this.context.Queryable() + this.sql = " (" + this.context.Queryable() .AS(tableName) .Where(string.Format(" {0}={1} ", whereLeft, whereRight)) - .Select("COUNT(1)").ToSql().Key+")>0 "; + .WhereIF(!string.IsNullOrEmpty(whereExpression), whereExpression) + .Select("COUNT(1)").ToSql().Key + ")>0 "; } else { this.sql = this.context.Queryable() .AS(tableName) .Where(string.Format(" {0}={1} ", whereLeft, whereRight)) + .WhereIF(!string.IsNullOrEmpty(whereExpression), whereExpression) .Select("COUNT(1)").ToSql().Key; } } + private string GetWhereExpression(MethodCallExpression methodCallExpression) + { + if (methodCallExpression.Arguments.Count <= 1) + return null; + var exp= methodCallExpression.Arguments[1]; + var querybuiler=InstanceFactory.GetQueryBuilder(this.context.CurrentConnectionConfig); + querybuiler.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.context.CurrentConnectionConfig); + querybuiler.Builder = InstanceFactory.GetSqlbuilder(this.context.CurrentConnectionConfig); + querybuiler.Builder.Context = querybuiler.Context; + querybuiler.Builder.QueryBuilder = querybuiler; + querybuiler.Context = this.context; + var expValue=querybuiler.GetExpressionValue(exp, ResolveExpressType.WhereMultiple); + var paramterName = (exp as LambdaExpression).Parameters[0].Name; + var sql = expValue.GetResultString(); + sql = sql.Replace(querybuiler.Builder.GetTranslationColumnName(paramterName) + ".", ""); + if (querybuiler.Parameters != null && querybuiler.Parameters.Count > 0) + { + foreach (var item in querybuiler.Parameters) + { + sql = sql.Replace(item.ParameterName,item.Value.ObjToString().ToSqlValue()); + } + } + return sql; + } + private MapperExpressionInfo GetSelectInfo(Expression expression) {