16
16
using System ;
17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
+ using System . Linq . Expressions ;
20
+ using System . Reflection ;
19
21
using MongoDB . Bson ;
20
22
21
23
namespace MongoDB . Driver . Linq
@@ -25,6 +27,7 @@ namespace MongoDB.Driver.Linq
25
27
/// </summary>
26
28
public static class LinqToMongo
27
29
{
30
+ // public static methods
28
31
/// <summary>
29
32
/// Determines whether a sequence contains all of the specified values.
30
33
/// </summary>
@@ -49,29 +52,6 @@ public static bool ContainsAny<TSource>(this IEnumerable<TSource> source, IEnume
49
52
return source . Any ( s => values . Contains ( s ) ) ;
50
53
}
51
54
52
-
53
- /// <summary>
54
- /// Determines whether a specified value is contained in a sequence.
55
- /// </summary>
56
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
57
- /// <param name="value">The value to locate in the sequence.</param>
58
- /// <param name="source">A sequence in which to locate the values.</param>
59
- /// <returns>True if the value is contained in the sequence.</returns>
60
- public static bool In < TSource > ( this TSource value , IEnumerable < TSource > source )
61
- {
62
- return source . Contains ( value ) ;
63
- }
64
-
65
- /// <summary>
66
- /// Injects a low level IMongoQuery into a LINQ where clause. Can only be used in LINQ queries.
67
- /// </summary>
68
- /// <param name="query">The low level query.</param>
69
- /// <returns>Throws an InvalidOperationException if called.</returns>
70
- public static bool Inject ( this IMongoQuery query )
71
- {
72
- throw new InvalidOperationException ( "The LinqToMongo.Inject method is only intended to be used in LINQ Where clauses." ) ;
73
- }
74
-
75
55
/// <summary>
76
56
/// Returns an explanation of how the query was executed (instead of the results).
77
57
/// </summary>
@@ -104,5 +84,60 @@ public static BsonDocument Explain<T>(this IQueryable<T> query, bool verbose)
104
84
}
105
85
return projector . Cursor . Explain ( verbose ) ;
106
86
}
87
+
88
+ /// <summary>
89
+ /// Determines whether a specified value is contained in a sequence.
90
+ /// </summary>
91
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
92
+ /// <param name="value">The value to locate in the sequence.</param>
93
+ /// <param name="source">A sequence in which to locate the values.</param>
94
+ /// <returns>True if the value is contained in the sequence.</returns>
95
+ public static bool In < TSource > ( this TSource value , IEnumerable < TSource > source )
96
+ {
97
+ return source . Contains ( value ) ;
98
+ }
99
+
100
+ /// <summary>
101
+ /// Injects a low level IMongoQuery into a LINQ where clause. Can only be used in LINQ queries.
102
+ /// </summary>
103
+ /// <param name="query">The low level query.</param>
104
+ /// <returns>Throws an InvalidOperationException if called.</returns>
105
+ public static bool Inject ( this IMongoQuery query )
106
+ {
107
+ throw new InvalidOperationException ( "The LinqToMongo.Inject method is only intended to be used in LINQ Where clauses." ) ;
108
+ }
109
+
110
+ /// <summary>
111
+ /// Sets an index hint on the query that's being built.
112
+ /// </summary>
113
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
114
+ /// <param name="query">The query being built.</param>
115
+ /// <param name="indexName">The name of the index to use.</param>
116
+ /// <returns>New query where the expression includes a WithIndex method call.</returns>
117
+ public static IQueryable < TSource > WithIndex < TSource > ( this IQueryable < TSource > query , string indexName )
118
+ {
119
+ return WithIndex ( query , ( BsonValue ) indexName ) ;
120
+ }
121
+
122
+ /// <summary>
123
+ /// Sets an index hint on the query that's being built.
124
+ /// </summary>
125
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
126
+ /// <param name="query">The query being built.</param>
127
+ /// <param name="indexHint">Hint for what index to use.</param>
128
+ /// <returns>New query where the expression includes a WithIndex method call.</returns>
129
+ public static IQueryable < TSource > WithIndex < TSource > ( this IQueryable < TSource > query , BsonDocument indexHint )
130
+ {
131
+ return WithIndex ( query , ( BsonValue ) indexHint ) ;
132
+ }
133
+
134
+ // private static methods
135
+ private static IQueryable < TSource > WithIndex < TSource > ( IQueryable < TSource > query , BsonValue index )
136
+ {
137
+ var method = ( ( MethodInfo ) MethodBase . GetCurrentMethod ( ) ) . MakeGenericMethod ( typeof ( TSource ) ) ;
138
+ var args = new [ ] { query . Expression , Expression . Constant ( index ) } ;
139
+ var expression = Expression . Call ( null , method , args ) ;
140
+ return query . Provider . CreateQuery < TSource > ( expression ) ;
141
+ }
107
142
}
108
143
}
0 commit comments