Skip to content

Commit aeb678a

Browse files
SMAH1SMAH1
andauthored
Linq first function support condition (#532)
* test to show a bug where First/FirstOrDefault doesn't work when it has a statement #530 * fix bug First/FirstOrDefault doesn't work when it has a statement #530 --------- Co-authored-by: SMAH1 <[email protected]>
1 parent e199b37 commit aeb678a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Redis.OM/Common/ExpressionTranslator.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ internal static RedisQuery BuildQueryFromExpression(Expression expression, Type
228228
case "First":
229229
case "Any":
230230
case "FirstOrDefault":
231+
if (exp.Arguments.Count > 1)
232+
{
233+
// Combine Where clause with existing query
234+
var condition = TranslateWhereMethod(exp, parameters, ref dialect);
235+
query.QueryText = query.QueryText == "*" ?
236+
condition :
237+
$"({condition} {query.QueryText})";
238+
query.Dialect = dialect;
239+
}
240+
231241
query.Limit ??= new SearchLimit { Offset = 0 };
232242
query.Limit.Number = 1;
233243
break;

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,59 @@ public void TestFirstOrDefault()
128128
Assert.Equal("John", firstJohn.Name);
129129
}
130130

131+
[Fact]
132+
public void TestFirstOrDefaultWithExpression()
133+
{
134+
var person = new Person[] {
135+
new Person { Name = "Q1", Age = 110 },
136+
new Person { Name = "Q2", Age = 112 },
137+
new Person { Name = "Q3", Age = 115 },
138+
new Person { Name = "Q1", Age = 120 },
139+
new Person { Name = "Q2", Age = 122 },
140+
new Person { Name = "Q1", Age = 130 },
141+
new Person { Name = "Q2", Age = 132 },
142+
};
143+
144+
var collection = new RedisCollection<Person>(_connection, 10000);
145+
IQueryable<Person> query = collection;
146+
147+
foreach (var p in person)
148+
{
149+
collection.Insert(p);
150+
}
151+
152+
var firstInCollection = collection.FirstOrDefault(x => x.Name == "Q3");
153+
var firstInQuery = query.FirstOrDefault(x => x.Name == "Q3");
154+
155+
Assert.NotNull(firstInCollection);
156+
Assert.NotNull(firstInQuery);
157+
158+
Assert.Equal("Q3", firstInCollection.Name);
159+
Assert.Equal("Q3", firstInQuery.Name);
160+
161+
firstInCollection = collection.FirstOrDefault(x => x.Name == "Q2" && x.Age > 115);
162+
firstInQuery = query.FirstOrDefault(x => x.Name == "Q2" && x.Age > 115);
163+
164+
Assert.NotNull(firstInCollection);
165+
Assert.NotNull(firstInQuery);
166+
167+
Assert.Equal("Q2", firstInCollection.Name);
168+
Assert.Equal("Q2", firstInQuery.Name);
169+
Assert.Equal(122, firstInCollection.Age);
170+
Assert.Equal(122, firstInQuery.Age);
171+
172+
firstInCollection = collection.Where(x => x.Age > 115).FirstOrDefault(x => x.Name == "Q1");
173+
firstInQuery = query.Where(x => x.Age > 115).FirstOrDefault(x => x.Name == "Q1");
174+
175+
Assert.NotNull(firstInCollection);
176+
Assert.NotNull(firstInQuery);
177+
178+
Assert.Equal("Q1", firstInCollection.Name);
179+
Assert.Equal("Q1", firstInQuery.Name);
180+
Assert.Equal(120, firstInCollection.Age);
181+
Assert.Equal(120, firstInQuery.Age);
182+
}
183+
131184
[Fact]
132185
public void TestAny()
133186
{

0 commit comments

Comments
 (0)