Skip to content

Commit

Permalink
Merge branch 'master' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
NorthernLight1 authored Jun 18, 2024
2 parents 1e491c0 + 1532ec4 commit 7a1fde7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,49 @@ public void Should_handle_prop_naming()
Assert.AreEqual("s.Id = t.Id AND s.ExternalId = t.ExternalId", sqlPredicate);
}

[TestMethod]
public void Should_handle_simple_big_one()
{
Expression<Func<Entity, Entity, bool>> expression = (s, t) => s.Type == t.Type &&
s.Id == t.Id &&
s.ExternalId == t.ExternalId &&
s.TesterVar1 == t.TesterVar1 &&
s.TesterVar2 == t.TesterVar2 &&
s.TesterVar3 == t.TesterVar3 &&
s.TesterVar4 == t.TesterVar4 &&
s.TesterVar5 == t.TesterVar5;

var sqlPredicate = expression.ToSqlPredicate("s", "t");

Assert.AreEqual("s.Type = t.Type AND s.Id = t.Id AND s.ExternalId = t.ExternalId AND s.TesterVar1 = t.TesterVar1 AND s.TesterVar2 = t.TesterVar2 AND s.TesterVar3 = t.TesterVar3 AND s.TesterVar4 = t.TesterVar4 AND s.TesterVar5 = t.TesterVar5", sqlPredicate);
}

[TestMethod]
public void Should_handle_complex_big_one()
{
Expression<Func<Entity, Entity, bool>> expression = (s, t) => s.Type == t.Type &&
s.Id == t.Id &&
(s.ExternalId == t.ExternalId || s.TesterVar1 == t.TesterVar1) &&
(s.TesterVar2 == t.TesterVar2 || (s.TesterVar2 == null && t.TesterVar2 == null)) &&
(s.TesterVar3 == t.TesterVar3 || (s.TesterVar3 != null && t.TesterVar3 != null));

var sqlPredicate = expression.ToSqlPredicate("s", "t");

Assert.AreEqual("s.Type = t.Type AND s.Id = t.Id AND (s.ExternalId = t.ExternalId OR s.TesterVar1 = t.TesterVar1) AND (s.TesterVar2 = t.TesterVar2 OR s.TesterVar2 IS NULL AND t.TesterVar2 IS NULL) AND (s.TesterVar3 = t.TesterVar3 OR s.TesterVar3 IS NOT NULL AND t.TesterVar3 IS NOT NULL)", sqlPredicate);
}

record Entity
{
public Guid Id { get; set; }
public EntityType Type { get; set; }
public int ExternalId { get; set; }
public string TesterVar1 { get; set; }
public string TesterVar2 { get; set; }
public string TesterVar3 { get; set; }
public string TesterVar4 { get; set; }
public string TesterVar5 { get; set; }
}

enum EntityType
{
One,
Expand Down
12 changes: 9 additions & 3 deletions N.EntityFrameworkCore.Extensions/Extensions/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,24 @@ internal static string ToSql(this Expression expression)
}
return sb.ToString();
}
internal static string ToSqlPredicate<T>(this Expression<T> expression, params string[] parameters)
{
var stringBuilder = new StringBuilder((string)expression.Body.GetPrivateFieldValue("DebugView"));
internal static string ToSqlPredicate<T>(this Expression<T> expression, params string[] parameters)
{
var expressionBody = (string)expression.Body.GetPrivateFieldValue("DebugView");
expressionBody = expressionBody.Replace(System.Environment.NewLine, " ");
var stringBuilder = new StringBuilder(expressionBody);

int i = 0;
foreach (var expressionParam in expression.Parameters)
{
if (parameters.Length <= i) break;
stringBuilder.Replace((string)expressionParam.GetPrivateFieldValue("DebugView"), parameters[i]);
i++;
}
stringBuilder.Replace("== null", "IS NULL");
stringBuilder.Replace("!= null", "IS NOT NULL");
stringBuilder.Replace("&&", "AND");
stringBuilder.Replace("==", "=");
stringBuilder.Replace("||", "OR");
stringBuilder.Replace("(System.Nullable`1[System.Int32])", "");
stringBuilder.Replace("(System.Int32)", "");
return stringBuilder.ToString();
Expand Down

0 comments on commit 7a1fde7

Please sign in to comment.