Skip to content

Commit 9d02950

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
support First(), FirstAsync(), FirstOrDefault(), FirstOrDefaultAsync(), Single(), SingleAsync(), SingleOrDefault(), SingleOrDefaultAsync()
1 parent 8173b8a commit 9d02950

File tree

16 files changed

+385
-32
lines changed

16 files changed

+385
-32
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ IQueryable<Product> solrLinq = solr.AsQueryable(setup =>
6161

6262
## Supported methods
6363

64-
### Top, Skip
6564
### OrderBy, OrderByDescending, ThenBy, ThenByDescending
6665
- Order by field
6766
```
@@ -103,5 +102,8 @@ IQueryable<Product> solrLinq = solr.AsQueryable(setup =>
103102
// cat:(qwe)
104103
Product[] result = solrLinq.Where(p => p.Categories.Any(s => s == "qwe")).ToArray();
105104
```
105+
### Top, Skip
106+
### First(), FirstAsync(), FirstOrDefault(), FirstOrDefaultAsync()
107+
### Single(), SingleAsync(), SingleOrDefault(), SingleOrDefaultAsync()
106108
## Nuget
107109
https://www.nuget.org/packages/SolrNet.Linq
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace SolrNet.Linq.IntegrationTests
6+
{
7+
public class EnumeratedTests
8+
{
9+
[Fact]
10+
public void HasResults()
11+
{
12+
Product t1 = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).First();
13+
string id = t1.Id;
14+
Product t2 = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).First(p => p.Id != "qwe");
15+
Product t3 = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).FirstOrDefault();
16+
Product t4 = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).FirstOrDefault(p => p.Id != "qwe");
17+
Product t5 = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).Single(p => p.Id == id);
18+
Product t6 = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).SingleOrDefault(p => p.Id == id);
19+
20+
Assert.NotNull(t1);
21+
Assert.NotNull(t2);
22+
Assert.NotNull(t3);
23+
Assert.NotNull(t4);
24+
Assert.NotNull(t5);
25+
Assert.NotNull(t6);
26+
27+
Assert.Equal(id, t2.Id);
28+
Assert.Equal(id, t3.Id);
29+
Assert.Equal(id, t4.Id);
30+
Assert.Equal(id, t5.Id);
31+
Assert.Equal(id, t6.Id);
32+
}
33+
34+
[Fact]
35+
public async Task HasResultsAsync()
36+
{
37+
Product t1 = await Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).FirstAsync();
38+
string id = t1.Id;
39+
Product t2 = await Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).FirstAsync(p => p.Id != "qwe");
40+
Product t3 = await Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).FirstOrDefaultAsync();
41+
Product t4 = await Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).FirstOrDefaultAsync(p => p.Id != "qwe");
42+
Product t5 = await Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).SingleAsync(p => p.Id == id);
43+
Product t6 = await Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Id).SingleOrDefaultAsync(p => p.Id == id);
44+
45+
Assert.NotNull(t1);
46+
Assert.NotNull(t2);
47+
Assert.NotNull(t3);
48+
Assert.NotNull(t4);
49+
Assert.NotNull(t5);
50+
Assert.NotNull(t6);
51+
52+
Assert.Equal(id, t2.Id);
53+
Assert.Equal(id, t3.Id);
54+
Assert.Equal(id, t4.Id);
55+
Assert.Equal(id, t5.Id);
56+
Assert.Equal(id, t6.Id);
57+
}
58+
59+
[Fact]
60+
public void OrDefault()
61+
{
62+
Product t1 = Product.SolrOperations.Value.AsQueryable().FirstOrDefault(p => p.Id == "qwe");
63+
Product t2 = Product.SolrOperations.Value.AsQueryable().SingleOrDefault(p => p.Id == "qwe");
64+
65+
Assert.Null(t1);
66+
Assert.Null(t2);
67+
}
68+
69+
[Fact]
70+
public async Task OrDefaultAsync()
71+
{
72+
Product t1 = await Product.SolrOperations.Value.AsQueryable().FirstOrDefaultAsync(p => p.Id == "qwe");
73+
Product t2 = await Product.SolrOperations.Value.AsQueryable().SingleOrDefaultAsync(p => p.Id == "qwe");
74+
75+
Assert.Null(t1);
76+
Assert.Null(t2);
77+
}
78+
}
79+
}

SolrNet.Linq.IntegrationTests/InitTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using Xunit;
62

7-
namespace SolrNet.Linq.IntegrationOData
3+
namespace SolrNet.Linq.IntegrationTests
84
{
95
public class InitTests
106
{

SolrNet.Linq.IntegrationTests/Product.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Net.Http.Headers;
43
using System.Runtime.Serialization;
54
using System.Threading;
65
using Microsoft.Extensions.DependencyInjection;
76
using SolrNet.Attributes;
8-
using SolrNet.Mapping;
97

10-
namespace SolrNet.Linq.IntegrationOData
8+
namespace SolrNet.Linq.IntegrationTests
119
{
1210
public class Product
1311
{

SolrNet.Linq.IntegrationTests/ResultTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading.Tasks;
44
using Xunit;
55

6-
namespace SolrNet.Linq.IntegrationOData
6+
namespace SolrNet.Linq.IntegrationTests
77
{
88
public class ResultTests
99
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Linq;
2+
using Xunit;
3+
4+
namespace SolrNet.Linq.IntegrationTests
5+
{
6+
public class SelectTests
7+
{
8+
//[Fact]
9+
//public void AnonimousClass()
10+
//{
11+
// var t1 = Product.SolrOperations.Value.AsQueryable().Select(p => new {p.Id, p.Price}).AsEnumerable()
12+
// .FirstOrDefault();
13+
14+
// Assert.NotNull(t1);
15+
//}
16+
}
17+
}

SolrNet.Linq.IntegrationTests/SortingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Linq;
33
using Xunit;
44

5-
namespace SolrNet.Linq.IntegrationOData
5+
namespace SolrNet.Linq.IntegrationTests
66
{
77
public class SortingTests
88
{

SolrNet.Linq.IntegrationTests/TakeSkipTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System.Linq;
32
using Xunit;
43

5-
namespace SolrNet.Linq.IntegrationOData
4+
namespace SolrNet.Linq.IntegrationTests
65
{
76
public class TakeSkipTests
87
{

SolrNet.Linq.IntegrationTests/WhereTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Linq;
22
using Xunit;
33

4-
namespace SolrNet.Linq.IntegrationOData
4+
namespace SolrNet.Linq.IntegrationTests
55
{
66
public class WhereTests
77
{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Linq;
3+
using System.Linq.Expressions;
4+
using System.Reflection;
5+
using SolrNet.Commands.Parameters;
6+
using SolrNet.Linq.Expressions.Context;
7+
8+
namespace SolrNet.Linq.Expressions
9+
{
10+
public static class EnumerateMethod
11+
{
12+
public const string FirstOrDefault = nameof(Queryable.FirstOrDefault);
13+
public const string First = nameof(Queryable.First);
14+
public const string Single = nameof(Queryable.Single);
15+
public const string SingleOrDefault = nameof(Queryable.SingleOrDefault);
16+
17+
public static EnumeratedResult TryVisitEnumerate(this MethodCallExpression node, QueryOptions options, MemberContext context)
18+
{
19+
EnumeratedResult result = EnumeratedResult.None;
20+
21+
if (node.Method.Name == FirstOrDefault)
22+
{
23+
result = EnumeratedResult.FirstOrDefault;
24+
}
25+
if (node.Method.Name == First)
26+
{
27+
result = EnumeratedResult.First;
28+
}
29+
if (node.Method.Name == SingleOrDefault)
30+
{
31+
result = EnumeratedResult.SingleOrDefault;
32+
}
33+
if (node.Method.Name == Single)
34+
{
35+
result = EnumeratedResult.Single;
36+
}
37+
38+
if (node.Method.DeclaringType == typeof(Queryable) && result != EnumeratedResult.None)
39+
{
40+
if (node.Arguments.Count == 2)
41+
{
42+
Expression arg = node.Arguments[1];
43+
44+
if (arg.NodeType == ExpressionType.Quote)
45+
{
46+
LambdaExpression lambda = (LambdaExpression)node.Arguments[1].StripQuotes();
47+
Expression whereMember = lambda.Body;
48+
49+
ISolrQuery filter = whereMember.GetSolrFilterQuery(context);
50+
options.FilterQueries.Add(filter);
51+
}
52+
else
53+
{
54+
throw new InvalidOperationException($"Unable to translate '{node.Method.Name}' method filter. Unexpected node type {arg.NodeType}");
55+
}
56+
}
57+
58+
options.Rows = 2;
59+
60+
if (result == EnumeratedResult.First || result == EnumeratedResult.FirstOrDefault)
61+
{
62+
options.Rows = 1;
63+
}
64+
}
65+
66+
return result;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)