|
4 | 4 | using System.Linq; |
5 | 5 | using System.Linq.Expressions; |
6 | 6 | using System.Reflection; |
| 7 | +using System.Runtime.CompilerServices; |
7 | 8 | using LinqToDB.Expressions; |
8 | 9 | using LinqToDB.Reflection; |
9 | 10 | using Microsoft.EntityFrameworkCore; |
@@ -60,19 +61,12 @@ public T[] GetAttributes<T>(Type type, bool inherit = true) where T : Attribute |
60 | 61 | var contextProp = Expression.Property(Expression.Convert(dcParam, typeof(LinqToDBForEFToolsDataConnection)), "Context"); |
61 | 62 | var filterBody = filter.Body.Transform(e => |
62 | 63 | { |
63 | | - switch (e) |
| 64 | + if (typeof(DbContext).IsSameOrParentOf(e.Type)) |
64 | 65 | { |
65 | | - case ConstantExpression cnt: |
66 | | - { |
67 | | - if (typeof(DbContext).IsSameOrParentOf(cnt.Type)) |
68 | | - { |
69 | | - Expression newExpr = contextProp; |
70 | | - if (newExpr.Type != cnt.Type) |
71 | | - newExpr = Expression.Convert(newExpr, cnt.Type); |
72 | | - return newExpr; |
73 | | - } |
74 | | - break; |
75 | | - } |
| 66 | + Expression newExpr = contextProp; |
| 67 | + if (newExpr.Type != e.Type) |
| 68 | + newExpr = Expression.Convert(newExpr, e.Type); |
| 69 | + return newExpr; |
76 | 70 | } |
77 | 71 |
|
78 | 72 | return e; |
@@ -314,6 +308,24 @@ public override void Print(ExpressionPrinter expressionPrinter) |
314 | 308 | { |
315 | 309 | expressionPrinter.Print(Expression); |
316 | 310 | } |
| 311 | + |
| 312 | + protected bool Equals(SqlTransparentExpression other) |
| 313 | + { |
| 314 | + return ReferenceEquals(this, other); |
| 315 | + } |
| 316 | + |
| 317 | + public override bool Equals(object obj) |
| 318 | + { |
| 319 | + if (ReferenceEquals(null, obj)) return false; |
| 320 | + if (ReferenceEquals(this, obj)) return true; |
| 321 | + if (obj.GetType() != this.GetType()) return false; |
| 322 | + return Equals((SqlTransparentExpression) obj); |
| 323 | + } |
| 324 | + |
| 325 | + public override int GetHashCode() |
| 326 | + { |
| 327 | + return RuntimeHelpers.GetHashCode(this); |
| 328 | + } |
317 | 329 | } |
318 | 330 |
|
319 | 331 | private Sql.ExpressionAttribute? GetDbFunctionFromMethodCall(Type type, MethodInfo methodInfo) |
@@ -390,7 +402,32 @@ private static EFCoreExpressionAttribute ConvertToExpressionAttribute(MemberInfo |
390 | 402 | { |
391 | 403 | string PrepareExpressionText(Expression? expr) |
392 | 404 | { |
393 | | - var idx = Array.IndexOf(parameters, expr); |
| 405 | + var idx = -1; |
| 406 | + |
| 407 | + for (var index = 0; index < parameters.Length; index++) |
| 408 | + { |
| 409 | + var param = parameters[index]; |
| 410 | + var found = ReferenceEquals(expr, param); |
| 411 | + if (!found) |
| 412 | + { |
| 413 | + if (param is SqlTransparentExpression transparent) |
| 414 | + { |
| 415 | + if (transparent.Expression is ConstantExpression constantExpr && |
| 416 | + expr is SqlConstantExpression sqlConstantExpr) |
| 417 | + { |
| 418 | + //found = sqlConstantExpr.Value.Equals(constantExpr.Value); |
| 419 | + found = true; |
| 420 | + } |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + if (found) |
| 425 | + { |
| 426 | + idx = index; |
| 427 | + break; |
| 428 | + } |
| 429 | + } |
| 430 | + |
394 | 431 | if (idx >= 0) |
395 | 432 | return $"{{{idx}}}"; |
396 | 433 |
|
@@ -455,7 +492,7 @@ private static Expression UnwrapConverted(Expression expr) |
455 | 492 | if (expr is SqlFunctionExpression func) |
456 | 493 | { |
457 | 494 | if (string.Equals(func.Name, "COALESCE", StringComparison.InvariantCultureIgnoreCase) && |
458 | | - func.Arguments.Count == 2 && func.Arguments[1].NodeType == ExpressionType.Default) |
| 495 | + func.Arguments.Count == 2 && func.Arguments[1].NodeType == ExpressionType.Extension) |
459 | 496 | return UnwrapConverted(func.Arguments[0]); |
460 | 497 | } |
461 | 498 |
|
|
0 commit comments