Skip to content

Commit 7e9fc55

Browse files
committed
- 修复 BaseEntity 子查询 .Where 问题;#1586
1 parent d2f4bfb commit 7e9fc55

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

Examples/base_entity/Program.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,16 +559,16 @@ static void Main(string[] args)
559559
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
560560
//.UseQuoteSqlName(false)
561561

562-
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")
562+
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")
563563

564-
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
564+
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
565565

566566
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
567567
////.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=toc;Pooling=true;Maximum Pool Size=2")
568568
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower)
569569

570-
.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2")
571-
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
570+
//.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2")
571+
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
572572

573573
//.UseConnectionString(FreeSql.DataType.Dameng, "server=127.0.0.1;port=5236;user=2user;password=123456789;database=2user;poolsize=5;")
574574
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
@@ -600,6 +600,15 @@ static void Main(string[] args)
600600
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
601601
#endregion
602602

603+
var xxxc1 = User1.Select.ToSql(a => new
604+
{
605+
count = User1.Where(b => b.Id == a.Id).Count()
606+
});
607+
608+
var tididentity = fsql.CodeFirst.GetTableByEntity(typeof(TestIdAndIdentity));
609+
610+
var tididDDL = fsql.CodeFirst.GetComparisonDDLStatements(typeof(TestIdAndIdentity));
611+
603612
var lstKafka = fsql.Ado.Query<ProducerModel_Kafka>("SELECT 123 AS ID,'10.0.0.0' AS IP FROM dual ");
604613

605614

@@ -2558,4 +2567,12 @@ public class ProducerModel_Kafka
25582567
/// </summary>
25592568
public ProducerConfig PConfig { get; set; }
25602569
}
2570+
2571+
class TestIdAndIdentity
2572+
{
2573+
[Column(IsPrimary = true)]
2574+
public int Id { get; set; }
2575+
[Column(IsIdentity = true)]
2576+
public int IdentityId { get; set; }
2577+
}
25612578
}

FreeSql/Internal/CommonExpression.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,31 +1255,34 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
12551255
}
12561256
}
12571257
}
1258-
if (new[] { "Where", "WhereIf" }.Contains(exp3tmpCall.Method.Name) && exp3tmpCall.Object != null)
1258+
if (new[] { "Where", "WhereIf" }.Contains(exp3tmpCall.Method.Name))
12591259
{
1260-
//这段特别兼容 DbSet.Where 表达式解析 #216
1261-
var exp3tmpTestCall = Expression.Call(exp3tmpCall.Object, exp3tmpCall.Method, exp3tmpCall.Arguments.Select(a =>
1260+
//if (exp3tmpCall.Object != null) //BaseEntity.Where 也需要兼容
12621261
{
1263-
var a2 = a;
1264-
if (a2.NodeType == ExpressionType.Quote) a2 = (a as UnaryExpression)?.Operand;
1265-
if (a2?.NodeType == ExpressionType.Lambda)
1262+
//这段特别兼容 DbSet.Where 表达式解析 #216
1263+
var exp3tmpTestCall = Expression.Call(exp3tmpCall.Object, exp3tmpCall.Method, exp3tmpCall.Arguments.Select(a =>
12661264
{
1267-
var alambda = a2 as LambdaExpression;
1268-
if (alambda.ReturnType == typeof(bool))
1269-
return Expression.Constant(null, a.Type);// Expression.Lambda(Expression.Constant(true), alambda.Parameters);
1270-
}
1271-
return a;
1272-
//if (a.Type == typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(exp3tmp.Type.GetGenericArguments()[0], typeof(bool))))
1273-
// return Expression.Lambda(Expression.Constant(true),
1274-
}).ToArray());
1275-
fsql = Expression.Lambda(exp3tmpTestCall).Compile().DynamicInvoke();
1276-
var fsqlFindMethod = fsql.GetType().GetMethod(exp3tmpCall.Method.Name, exp3tmpCall.Arguments.Select(a => a.Type).ToArray());
1277-
if (fsqlFindMethod == null)
1278-
throw new Exception(CoreStrings.Unable_Parse_ExpressionMethod(exp3tmpCall.Method.Name));
1279-
var exp3StackOld = exp3Stack;
1280-
exp3Stack = new Stack<Expression>();
1281-
exp3Stack.Push(Expression.Call(Expression.Constant(fsql), fsqlFindMethod, exp3tmpCall.Arguments));
1282-
while (exp3StackOld.Any()) exp3Stack.Push(exp3StackOld.Pop());
1265+
var a2 = a;
1266+
if (a2.NodeType == ExpressionType.Quote) a2 = (a as UnaryExpression)?.Operand;
1267+
if (a2?.NodeType == ExpressionType.Lambda)
1268+
{
1269+
var alambda = a2 as LambdaExpression;
1270+
if (alambda.ReturnType == typeof(bool))
1271+
return Expression.Constant(null, a.Type);// Expression.Lambda(Expression.Constant(true), alambda.Parameters);
1272+
}
1273+
return a;
1274+
//if (a.Type == typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(exp3tmp.Type.GetGenericArguments()[0], typeof(bool))))
1275+
// return Expression.Lambda(Expression.Constant(true),
1276+
}).ToArray());
1277+
fsql = Expression.Lambda(exp3tmpTestCall).Compile().DynamicInvoke();
1278+
var fsqlFindMethod = fsql.GetType().GetMethod(exp3tmpCall.Method.Name, exp3tmpCall.Arguments.Select(a => a.Type).ToArray());
1279+
if (fsqlFindMethod == null)
1280+
throw new Exception(CoreStrings.Unable_Parse_ExpressionMethod(exp3tmpCall.Method.Name));
1281+
var exp3StackOld = exp3Stack;
1282+
exp3Stack = new Stack<Expression>();
1283+
exp3Stack.Push(Expression.Call(Expression.Constant(fsql), fsqlFindMethod, exp3tmpCall.Arguments));
1284+
while (exp3StackOld.Any()) exp3Stack.Push(exp3StackOld.Pop());
1285+
}
12831286
}
12841287
}
12851288
if (fsql == null)

0 commit comments

Comments
 (0)