Skip to content

Commit b49806e

Browse files
authored
Adding Any method without arguments to IRedisCollection (#270)
1 parent becd81e commit b49806e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Redis.OM/Searching/IRedisCollection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public interface IRedisCollection<T> : IOrderedQueryable<T>, IAsyncEnumerable<T>
101101
/// <returns>the item if it's present.</returns>
102102
T? FindById(string id);
103103

104+
/// <summary>
105+
/// Checks to see if the collection contains any.
106+
/// </summary>
107+
/// <returns>Whether anything matching the expression was found.</returns>
108+
bool Any();
109+
104110
/// <summary>
105111
/// Checks to see if anything matching the expression exists.
106112
/// </summary>

src/Redis.OM/Searching/RedisCollection.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ internal Expression<Func<T, bool>>? BooleanExpression
113113
}
114114
}
115115

116+
/// <inheritdoc />
117+
public bool Any()
118+
{
119+
var query = ExpressionTranslator.BuildQueryFromExpression(Expression, typeof(T), BooleanExpression);
120+
query.Limit = new SearchLimit { Number = 0, Offset = 0 };
121+
return (int)_connection.Search<T>(query).DocumentCount > 0;
122+
}
123+
116124
/// <summary>
117125
/// Checks to see if anything matching the expression exists.
118126
/// </summary>

test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,5 +2547,33 @@ public void SearchTagFieldAndTextListContainsWithEscapes()
25472547
"0",
25482548
"100"));
25492549
}
2550+
2551+
[Fact]
2552+
public void SearchWithEmptyAny()
2553+
{
2554+
_mock.Setup(x => x.Execute(It.IsAny<string>(), It.IsAny<string[]>()))
2555+
.Returns(_mockReply);
2556+
var collection = new RedisCollection<Person>(_mock.Object);
2557+
var any = collection.Any();
2558+
_mock.Verify(x => x.Execute(
2559+
"FT.SEARCH",
2560+
"person-idx",
2561+
"*",
2562+
"LIMIT",
2563+
"0",
2564+
"0"));
2565+
Assert.True(any);
2566+
2567+
any = collection.Where(x => x.TagField == "foo").Any();
2568+
_mock.Verify(x => x.Execute(
2569+
"FT.SEARCH",
2570+
"person-idx",
2571+
"(@TagField:{foo})",
2572+
"LIMIT",
2573+
"0",
2574+
"0"));
2575+
2576+
Assert.True(any);
2577+
}
25502578
}
25512579
}

0 commit comments

Comments
 (0)