diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 6f9d286fe..d6e65c9a0 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -94,6 +94,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index c7724f19e..b876930b7 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UCustom01.Init(); UConfig(); DeleteTest(); Fastest2(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom01.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom01.cs new file mode 100644 index 000000000..4a2fe3902 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom01.cs @@ -0,0 +1,71 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UCustom01 + { + + public static void Init() + { + var db = new SqlSugarScope(new SqlSugar.ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true + }); + db.Aop.OnLogExecuted = (s, p) => + { + Console.WriteLine(s); + }; + //建表 + if (!db.DbMaintenance.IsAnyTable("User_Test001", false)) + { + db.CodeFirst.InitTables(); + } + if (!db.DbMaintenance.IsAnyTable("UserRole_Test001", false)) + { + db.CodeFirst.InitTables(); + } + + //用例代码 + var result = db.Queryable((u, ur) => new object[] { + + JoinType.Left,u.ID==ur.UserID + + + + }).Select((u, ur) => new + + { + + customName = SqlFunc.Subqueryable().Where(s => s.UserName == u.UserName).Select(s => s.UserName+"") + + + + }).ToPageList(1, 10); + + Console.WriteLine(result); + Console.WriteLine("用例跑完"); + Console.ReadKey(); + } + [SugarTable("unitUser_Test001")] + public class User_Test001 + { + + public int ID { get; set; } + public string UserName { get; set; } + } + [SugarTable("unitUserRole_Test001")] + public class UserRole_Test001 + { + + public int ID { get; set; } + public int UserID { get; set; } + } + } +} diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ExpressionContext.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ExpressionContext.cs index 7d16573a7..1f514fb44 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ExpressionContext.cs @@ -39,6 +39,7 @@ namespace SqlSugar public int Index { get; set; } public int ParameterIndex { get; set; } public string SingleTableNameSubqueryShortName{ get; set; } + public string CurrentShortName { get; set; } public MappingColumnList MappingColumns { get; set; } public MappingTableList MappingTables { get; set; } public IgnoreColumnList IgnoreComumnList { get; set; } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs index 11869c486..f11b95ed7 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs @@ -49,10 +49,28 @@ namespace SqlSugar this.Context.InitMappingInfo(entityType); this.Context.RefreshMapping(); } - if(this.Context.JoinIndex==0) - return SubTools.GetMethodValue(this.Context, exp.Arguments[0],ResolveExpressType.FieldSingle); + var result = ""; + if (this.Context.JoinIndex == 0) + result = SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldSingle); else - return SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldMultiple); + result = SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldMultiple); + + SetShortName(exp, result); + + return result; + } + + private void SetShortName(MethodCallExpression exp, string result) + { + if (exp.Arguments[0] is LambdaExpression && result.IsContainsIn("+", "-")) + { + var parameters = (exp.Arguments[0] as LambdaExpression).Parameters; + if (parameters != null && parameters.Count > 0) + { + this.Context.CurrentShortName = this.Context.SqlTranslationLeft + parameters[0] + this.Context.SqlTranslationRight; + } + + } } } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs index 8bbf94830..2227f3950 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs @@ -96,6 +96,10 @@ namespace SqlSugar { GetSubAs(sqlItems, asItems); } + if (this.context.CurrentShortName.HasValue()) + { + GetShortName(sqlItems); + } var sql = string.Join(UtilConstants.Space, sqlItems); return this.context.DbMehtods.Pack(sql); } @@ -112,6 +116,16 @@ namespace SqlSugar } } } + private void GetShortName(List sqlItems) + { + for (int i = 0; i < sqlItems.Count; i++) + { + if (sqlItems[i].StartsWith("FROM " + this.context.SqlTranslationLeft)) + { + sqlItems[i] = sqlItems[i]+" "+this.context.CurrentShortName +" "; + } + } + } private List GetSubItems() {