Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Fix issue with ToLowerInvariant() method in LiteDBRepository.cs Setup failed: Method ToLowerInvariant() in String are not supported when convert to BsonExpression #479

supporte0c1d6743057
diff --git a/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs b/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
index 76577dd79..84ec6f25f 100644
--- a/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
+++ b/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
@@ -100,7 +100,10 @@ namespace Grand.Data.LiteDb
/// <returns>Entity</returns>
public virtual Task<T> GetOneAsync(Expression<Func<T, bool>> predicate)
{
- return Task.FromResult(_collection.Find(predicate).FirstOrDefault());
+ var visitor = new ToLowerInvariantVisitor();
+ //WORKAROUND Issue #479 - Method ToLowerInvariant() in String are not supported when convert to BsonExpression
+ var modifiedExpression = (Expression<Func<T, bool>>)visitor.Visit(predicate);
+ return Task.FromResult(_collection.Find(modifiedExpression).FirstOrDefault());
}
/// <summary>
diff --git a/src/Core/Grand.Data/LiteDb/ToLowerInvariantVisitor.cs b/src/Core/Grand.Data/LiteDb/ToLowerInvariantVisitor.cs
new file mode 100644
index 000000000..ed5bd4c5c
--- /dev/null
+++ b/src/Core/Grand.Data/LiteDb/ToLowerInvariantVisitor.cs
@@ -0,0 +1,19 @@
+using System.Linq.Expressions;
+
+namespace Grand.Data.LiteDb;
+public class ToLowerInvariantVisitor : ExpressionVisitor
+{
+ protected override Expression VisitMethodCall(MethodCallExpression node)
+ {
+ if (node.Method == typeof(string).GetMethod("ToLowerInvariant", Type.EmptyTypes))
+ {
+ var invariantMethod = typeof(string).GetMethod("ToLower", Type.EmptyTypes);
+ if (invariantMethod != null)
+ {
+ return Expression.Call(node.Object, invariantMethod);
+ }
+
+ }
+ return base.VisitMethodCall(node);
+ }
+}
diff --git a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
index 8c0db646c..b140dfec2 100644
--- a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
+++ b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
@@ -66,7 +66,7 @@ namespace Grand.Data.Tests.LiteDb
}
[TestMethod()]
- public async Task GetOneAsyncAsync_LiteRepository_Success()
+ public async Task GetOneAsync_LiteRepository_Success()
{
var myRepository = new LiteDBRepositoryMock<SampleCollection>();
var product = new SampleCollection { Id = "1" };
@@ -76,7 +76,17 @@ namespace Grand.Data.Tests.LiteDb
Assert.IsNotNull(p);
}
+ [TestMethod()]
+ public async Task GetOneAsync_ToLowerInvariant_LiteRepository_Success()
+ {
+ var myRepository = new LiteDBRepositoryMock<SampleCollection>();
+ var product = new SampleCollection { Id = "1" };
+ await myRepository.InsertAsync(product);
+
+ var p = await myRepository.GetOneAsync(x => x.Id == "1".ToLowerInvariant());
+ Assert.IsNotNull(p);
+ }
[TestMethod()]
public async Task ClearAsync_LiteRepository_Success()
{