Skip to content

Commit

Permalink
add support for Contains in IEnumerable litedb-org#652
Browse files Browse the repository at this point in the history
  • Loading branch information
Rytis Ilciukas committed Jul 18, 2017
1 parent 34e8a79 commit 74222c8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
58 changes: 58 additions & 0 deletions LiteDB.Tests/Database/LinqContainsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;

namespace LiteDB.Tests.Database
{
[TestClass]
public class LinqContainsTests
{
private TempFile _tempFile;
private LiteDatabase _database;
private LiteCollection<ItemWithEnumerable> _collection;

public class ItemWithEnumerable
{
public int[] Array { get; set; }

public IEnumerable<int> Enumerable { get; set; }
}

[TestInitialize]
public void Init()
{
_tempFile = new TempFile();
_database = new LiteDatabase(_tempFile.Filename);
_collection = _database.GetCollection<ItemWithEnumerable>("items");

var item = new ItemWithEnumerable()
{
Array = new int[] { 1 },
Enumerable = new List<int>() { 2 },
};

_collection.Insert(item);
}

[TestCleanup]
public void Cleanup()
{
_database.Dispose();
_tempFile.Dispose();
}

[TestMethod]
public void ArrayContains()
{
var result = _collection.Find(i => i.Array.Contains(1)).ToList();
Assert.AreEqual(1, result.Count);
}

[TestMethod]
public void ListContains()
{
var result = _collection.Find(i => i.Enumerable.Contains(2)).ToList();
Assert.AreEqual(1, result.Count);
}
}
}
1 change: 1 addition & 0 deletions LiteDB.Tests/LiteDB.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="Concurrency\ProcessTest.cs" />
<Compile Include="Database\BigDatabaseTest.cs" />
<Compile Include="Database\AutoIndexDatabaseTest.cs" />
<Compile Include="Database\LinqContainsTests.cs" />
<Compile Include="Database\LockTimeoutTests.cs" />
<Compile Include="Engine\JournalTest.cs" />
<Compile Include="Database\ULongListTest.cs" />
Expand Down
10 changes: 8 additions & 2 deletions LiteDB/Mapper/Linq/QueryVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ParameterDictionary = System.Collections.Generic.Dictionary<System.Linq.Expressions.ParameterExpression, LiteDB.BsonValue>;

namespace LiteDB
Expand Down Expand Up @@ -159,6 +157,14 @@ private Query VisitExpression(Expression expr, string prefix = null)

return Query.EQ(field, value);
}
else if (method == "Contains" && typeof(IEnumerable).IsAssignableFrom(type))
{
var field = this.GetField(met.Object, prefix);
var value = this.VisitValue(met.Arguments[0], null);

return Query.EQ(field, value);
}

// Any (Enumerable): x.Customer.Any(z => z.Name.StartsWith("John"))
else if(method == "Any" && type == typeof(Enumerable) && paramType == ExpressionType.Parameter)
{
Expand Down

0 comments on commit 74222c8

Please sign in to comment.