Skip to content

Commit a06d990

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
support count method
1 parent 2febee8 commit a06d990

File tree

7 files changed

+145
-3
lines changed

7 files changed

+145
-3
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ IQueryable<Product> solrLinq = solr.AsQueryable(setup =>
105105
### Paging
106106
- Top
107107
- Skip
108+
### Any
109+
- Any
110+
- AnyAsync
111+
### Count
112+
- Count
113+
- CountAsync
114+
- LongCount
115+
- LongCountAsync
108116
### First
109117
- First
110118
- FirstAsync

SolrNet.Linq.IntegrationTests/EnumeratedTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,43 @@ public async Task OrDefaultAsync()
101101
Assert.Null(t3);
102102
Assert.Null(t4);
103103
}
104+
105+
[Fact]
106+
public async Task Any()
107+
{
108+
Assert.True(await Product.SolrOperations.Value.AsQueryable().AnyAsync(p => p.Id != "qwe"));
109+
Assert.True(await Product.SolrOperations.Value.AsQueryable().AnyAsync());
110+
Assert.True(Product.SolrOperations.Value.AsQueryable().Any());
111+
Assert.True(Product.SolrOperations.Value.AsQueryable().Any(p => p.Id != "qwe"));
112+
113+
Assert.False(await Product.SolrOperations.Value.AsQueryable().AnyAsync(p => p.Id == "qwe"));
114+
Assert.False(await Product.SolrOperations.Value.AsQueryable().Where(p => p.Id == "qwe").AnyAsync());
115+
Assert.False(Product.SolrOperations.Value.AsQueryable().Where(p => p.Id == "qwe").Any());
116+
Assert.False(Product.SolrOperations.Value.AsQueryable().Any(p => p.Id == "qwe"));
117+
}
118+
119+
[Fact]
120+
public async Task CountLongCount()
121+
{
122+
var c1 = await Product.SolrOperations.Value.AsQueryable().CountAsync(p => p.Id != "qwe");
123+
var c2 = await Product.SolrOperations.Value.AsQueryable().LongCountAsync(p => p.Id != "qwe");
124+
var c3 = Product.SolrOperations.Value.AsQueryable().LongCount(p => p.Id != "qwe");
125+
var c4 = Product.SolrOperations.Value.AsQueryable().Count(p => p.Id != "qwe");
126+
127+
Assert.True(c1 > 0);
128+
Assert.Equal(c1, c2);
129+
Assert.Equal(c1, c3);
130+
Assert.Equal(c1, c4);
131+
132+
var c5 = await Product.SolrOperations.Value.AsQueryable().CountAsync(p => p.Id == "qwe");
133+
var c6 = await Product.SolrOperations.Value.AsQueryable().LongCountAsync(p => p.Id == "qwe");
134+
var c7 = Product.SolrOperations.Value.AsQueryable().LongCount(p => p.Id == "qwe");
135+
var c8 = Product.SolrOperations.Value.AsQueryable().Count(p => p.Id == "qwe");
136+
137+
Assert.Equal(0, c1);
138+
Assert.Equal(0, c2);
139+
Assert.Equal(0, c3);
140+
Assert.Equal(0, c4);
141+
}
104142
}
105143
}

SolrNet.Linq/Expressions/EnumerateMethod.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public static class EnumerateMethod
1313
public const string First = nameof(Queryable.First);
1414
public const string Single = nameof(Queryable.Single);
1515
public const string SingleOrDefault = nameof(Queryable.SingleOrDefault);
16+
public const string Any = nameof(Queryable.Any);
17+
public const string Count = nameof(Queryable.Count);
18+
public const string LongCount = nameof(Queryable.LongCount);
1619

1720
public static EnumeratedResult TryVisitEnumerate(this MethodCallExpression node, QueryOptions options, MemberContext context)
1821
{
@@ -34,6 +37,18 @@ public static EnumeratedResult TryVisitEnumerate(this MethodCallExpression node,
3437
{
3538
result = EnumeratedResult.Single;
3639
}
40+
if (node.Method.Name == Any)
41+
{
42+
result = EnumeratedResult.Any;
43+
}
44+
if (node.Method.Name == Count)
45+
{
46+
result = EnumeratedResult.Count;
47+
}
48+
if (node.Method.Name == LongCount)
49+
{
50+
result = EnumeratedResult.Count;
51+
}
3752

3853
if (node.Method.DeclaringType == typeof(Queryable) && result != EnumeratedResult.None)
3954
{
@@ -61,6 +76,11 @@ public static EnumeratedResult TryVisitEnumerate(this MethodCallExpression node,
6176
{
6277
options.Rows = 1;
6378
}
79+
80+
if (result == EnumeratedResult.Any || result == EnumeratedResult.Count)
81+
{
82+
options.Rows = 0;
83+
}
6484
}
6585

6686
return result;

SolrNet.Linq/Expressions/EnumeratedResult.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public enum EnumeratedResult
66
First = 1,
77
FirstOrDefault = 2,
88
Single = 3,
9-
SingleOrDefault = 4
9+
SingleOrDefault = 4,
10+
Any = 5,
11+
Count = 6
1012
}
1113
}

SolrNet.Linq/SolrLinqExtensions.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,78 @@ namespace SolrNet.Linq
99
{
1010
public static class SolrLinqExtensions
1111
{
12+
public static async Task<long> LongCountAsync<TSource>(this IQueryable<TSource> query)
13+
{
14+
return await query.CountAsync();
15+
}
16+
17+
public static async Task<long> LongCountAsync<TSource>(this IQueryable<TSource> query, Expression<Func<TSource, bool>> predicate)
18+
{
19+
return await query.LongCountAsync();
20+
}
21+
22+
public static async Task<int> CountAsync<TSource>(this IQueryable<TSource> query)
23+
{
24+
if (query.Provider is SolrQueryProvider<TSource> provider)
25+
{
26+
MethodCallExpression mce = Expression.Call(
27+
null,
28+
GetMethod<TSource>(nameof(Queryable.Count), 1),
29+
query.Expression);
30+
31+
var result = await provider.ExecuteAsync<int>(mce);
32+
33+
return result;
34+
}
35+
36+
return query.Count();
37+
}
38+
39+
public static async Task<int> CountAsync<TSource>(this IQueryable<TSource> query, Expression<Func<TSource, bool>> predicate)
40+
{
41+
if (query.Provider is SolrQueryProvider<TSource> provider)
42+
{
43+
var result = await provider.ExecuteAsync<int>(Expression.Call(
44+
null,
45+
GetMethod<TSource>(nameof(Queryable.Any), 2), query.Expression, predicate));
46+
47+
return result;
48+
}
49+
50+
return query.Count(predicate);
51+
}
52+
53+
public static async Task<bool> AnyAsync<TSource>(this IQueryable<TSource> query)
54+
{
55+
if (query.Provider is SolrQueryProvider<TSource> provider)
56+
{
57+
MethodCallExpression mce = Expression.Call(
58+
null,
59+
GetMethod<TSource>(nameof(Queryable.Any), 1),
60+
query.Expression);
61+
62+
bool result = await provider.ExecuteAsync<bool>(mce);
63+
64+
return result;
65+
}
66+
67+
return query.Any();
68+
}
69+
70+
public static async Task<bool> AnyAsync<TSource>(this IQueryable<TSource> query, Expression<Func<TSource, bool>> predicate)
71+
{
72+
if (query.Provider is SolrQueryProvider<TSource> provider)
73+
{
74+
bool result = await provider.ExecuteAsync<bool>(Expression.Call(
75+
null,
76+
GetMethod<TSource>(nameof(Queryable.Any), 2), query.Expression, predicate));
77+
78+
return result;
79+
}
80+
81+
return query.Any(predicate);
82+
}
83+
1284
public static async Task<TSource> FirstAsync<TSource>(this IQueryable<TSource> query)
1385
{
1486
if (query.Provider is SolrQueryProvider<TSource> provider)

SolrNet.Linq/SolrNet.Linq.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<PackageTags>solr solrnet linq</PackageTags>
1313
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1414
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
15-
<Version>1.2.1</Version>
16-
<PackageReleaseNotes>support First(), FirstOrDefault(). Single(), SingleOrDefault() methods and its async versions</PackageReleaseNotes>
15+
<Version>1.3.0</Version>
16+
<PackageReleaseNotes>support Any(), Count(), LongCount() methods and its async versions</PackageReleaseNotes>
1717
</PropertyGroup>
1818

1919
<ItemGroup>

SolrNet.Linq/SolrQueryProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ private static object HandleResults(Tuple<ISolrQuery, QueryOptions, EnumeratedRe
6767
case EnumeratedResult.FirstOrDefault: return solrQueryResults.FirstOrDefault();
6868
case EnumeratedResult.Single: return solrQueryResults.Single();
6969
case EnumeratedResult.SingleOrDefault: return solrQueryResults.SingleOrDefault();
70+
case EnumeratedResult.Any: return solrQueryResults.NumFound > 0;
71+
case EnumeratedResult.Count: return solrQueryResults.NumFound;
7072

7173
default: return solrQueryResults;
7274
}

0 commit comments

Comments
 (0)