@@ -57,6 +57,11 @@ public static class IAsyncCursorSourceExtensions
57
57
/// <returns>True if the cursor contains any documents.</returns>
58
58
public static bool Any < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
59
59
{
60
+ if ( source is IQueryable < TDocument > queryable && ! cancellationToken . CanBeCanceled )
61
+ {
62
+ return Queryable . Any ( queryable ) ;
63
+ }
64
+
60
65
using ( var cursor = source . ToCursor ( cancellationToken ) )
61
66
{
62
67
return cursor . Any ( cancellationToken ) ;
@@ -72,6 +77,11 @@ public static class IAsyncCursorSourceExtensions
72
77
/// <returns>A Task whose result is true if the cursor contains any documents.</returns>
73
78
public static async Task < bool > AnyAsync < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
74
79
{
80
+ if ( source is IMongoQueryableForwarder < TDocument > queryableForwarder )
81
+ {
82
+ return await queryableForwarder . AnyAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
83
+ }
84
+
75
85
using ( var cursor = await source . ToCursorAsync ( cancellationToken ) . ConfigureAwait ( false ) )
76
86
{
77
87
return await cursor . AnyAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -87,6 +97,11 @@ public static class IAsyncCursorSourceExtensions
87
97
/// <returns>The first document.</returns>
88
98
public static TDocument First < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
89
99
{
100
+ if ( source is IQueryable < TDocument > queryable && ! cancellationToken . CanBeCanceled )
101
+ {
102
+ return Queryable . First ( queryable ) ;
103
+ }
104
+
90
105
using ( var cursor = source . ToCursor ( cancellationToken ) )
91
106
{
92
107
return cursor . First ( cancellationToken ) ;
@@ -102,6 +117,11 @@ public static class IAsyncCursorSourceExtensions
102
117
/// <returns>A Task whose result is the first document.</returns>
103
118
public static async Task < TDocument > FirstAsync < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
104
119
{
120
+ if ( source is IMongoQueryableForwarder < TDocument > queryableForwarder )
121
+ {
122
+ return await queryableForwarder . FirstAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
123
+ }
124
+
105
125
using ( var cursor = await source . ToCursorAsync ( cancellationToken ) . ConfigureAwait ( false ) )
106
126
{
107
127
return await cursor . FirstAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -117,6 +137,11 @@ public static class IAsyncCursorSourceExtensions
117
137
/// <returns>The first document of the cursor, or a default value if the cursor contains no documents.</returns>
118
138
public static TDocument FirstOrDefault < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
119
139
{
140
+ if ( source is IQueryable < TDocument > queryable && ! cancellationToken . CanBeCanceled )
141
+ {
142
+ return Queryable . FirstOrDefault ( queryable ) ;
143
+ }
144
+
120
145
using ( var cursor = source . ToCursor ( cancellationToken ) )
121
146
{
122
147
return cursor . FirstOrDefault ( cancellationToken ) ;
@@ -132,6 +157,11 @@ public static class IAsyncCursorSourceExtensions
132
157
/// <returns>A Task whose result is the first document of the cursor, or a default value if the cursor contains no documents.</returns>
133
158
public static async Task < TDocument > FirstOrDefaultAsync < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
134
159
{
160
+ if ( source is IMongoQueryableForwarder < TDocument > queryableForwarder )
161
+ {
162
+ return await queryableForwarder . FirstOrDefaultAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
163
+ }
164
+
135
165
using ( var cursor = await source . ToCursorAsync ( cancellationToken ) . ConfigureAwait ( false ) )
136
166
{
137
167
return await cursor . FirstOrDefaultAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -221,6 +251,11 @@ public static class IAsyncCursorSourceExtensions
221
251
/// <returns>The only document of a cursor.</returns>
222
252
public static TDocument Single < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
223
253
{
254
+ if ( source is IQueryable < TDocument > queryable && ! cancellationToken . CanBeCanceled )
255
+ {
256
+ return Queryable . Single ( queryable ) ;
257
+ }
258
+
224
259
using ( var cursor = source . ToCursor ( cancellationToken ) )
225
260
{
226
261
return cursor . Single ( cancellationToken ) ;
@@ -236,6 +271,11 @@ public static class IAsyncCursorSourceExtensions
236
271
/// <returns>A Task whose result is the only document of a cursor.</returns>
237
272
public static async Task < TDocument > SingleAsync < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
238
273
{
274
+ if ( source is IMongoQueryableForwarder < TDocument > queryableForwarder )
275
+ {
276
+ return await queryableForwarder . SingleAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
277
+ }
278
+
239
279
using ( var cursor = await source . ToCursorAsync ( cancellationToken ) . ConfigureAwait ( false ) )
240
280
{
241
281
return await cursor . SingleAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -252,6 +292,11 @@ public static class IAsyncCursorSourceExtensions
252
292
/// <returns>The only document of a cursor, or a default value if the cursor contains no documents.</returns>
253
293
public static TDocument SingleOrDefault < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
254
294
{
295
+ if ( source is IQueryable < TDocument > queryable && ! cancellationToken . CanBeCanceled )
296
+ {
297
+ return Queryable . SingleOrDefault ( queryable ) ;
298
+ }
299
+
255
300
using ( var cursor = source . ToCursor ( cancellationToken ) )
256
301
{
257
302
return cursor . SingleOrDefault ( cancellationToken ) ;
@@ -268,6 +313,11 @@ public static class IAsyncCursorSourceExtensions
268
313
/// <returns>A Task whose result is the only document of a cursor, or a default value if the cursor contains no documents.</returns>
269
314
public static async Task < TDocument > SingleOrDefaultAsync < TDocument > ( this IAsyncCursorSource < TDocument > source , CancellationToken cancellationToken = default ( CancellationToken ) )
270
315
{
316
+ if ( source is IMongoQueryableForwarder < TDocument > queryableForwarder )
317
+ {
318
+ return await queryableForwarder . SingleOrDefaultAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
319
+ }
320
+
271
321
using ( var cursor = await source . ToCursorAsync ( cancellationToken ) . ConfigureAwait ( false ) )
272
322
{
273
323
return await cursor . SingleOrDefaultAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
0 commit comments