Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CompiledQuery.cs « Objects « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96f6bb6be5c8f065c7afe966afcf6f67516d7c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//---------------------------------------------------------------------
// <copyright file="CompiledQuery.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner [....]
// @backupowner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Objects.ELinq;
using System.Diagnostics;
using System.Data.Objects.Internal;
using OM = System.Collections.ObjectModel;

namespace System.Data.Objects
{
    /// <summary>
    /// Caches an ELinq query
    /// </summary>
    public sealed class CompiledQuery
    {
        // NOTE: make sure all changes to this object keep it immutable
        //       so it won't have any thread saftey concerns
        private readonly LambdaExpression _query;
        private readonly Guid _cacheToken = Guid.NewGuid();

        /// <summary>
        /// Constructs a new compiled query instance which hosts the delegate returned to the user
        /// (one of the Invoke overloads).
        /// </summary>
        /// <param name="query">Compiled query expression.</param>
        /// <param name="parameterDelegateType">The type of the delegate producing parameter values from CompiledQuery
        /// delegate arguments. For details, see CompiledQuery.Parameter.CreateObjectParameter.</param>
        private CompiledQuery(LambdaExpression query)
        {
            EntityUtil.CheckArgumentNull(query, "query");

            // lockdown the query (all closures become constants)
            Funcletizer funcletizer = Funcletizer.CreateCompiledQueryLockdownFuncletizer();
            Func<bool> recompiledRequire;
            _query = (LambdaExpression)funcletizer.Funcletize(query, out recompiledRequire);
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TArg9">The scalar type of parameter 9.</typeparam>
        /// <typeparam name="TArg10">The scalar type of parameter 10.</typeparam>
        /// <typeparam name="TArg11">The scalar type of parameter 11.</typeparam>
        /// <typeparam name="TArg12">The scalar type of parameter 12.</typeparam>
        /// <typeparam name="TArg13">The scalar type of parameter 13.</typeparam>
        /// <typeparam name="TArg14">The scalar type of parameter 14.</typeparam>
        /// <typeparam name="TArg15">The scalar type of parameter 15.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TArg9">The scalar type of parameter 9.</typeparam>
        /// <typeparam name="TArg10">The scalar type of parameter 10.</typeparam>
        /// <typeparam name="TArg11">The scalar type of parameter 11.</typeparam>
        /// <typeparam name="TArg12">The scalar type of parameter 12.</typeparam>
        /// <typeparam name="TArg13">The scalar type of parameter 13.</typeparam>
        /// <typeparam name="TArg14">The scalar type of parameter 14.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TArg9">The scalar type of parameter 9.</typeparam>
        /// <typeparam name="TArg10">The scalar type of parameter 10.</typeparam>
        /// <typeparam name="TArg11">The scalar type of parameter 11.</typeparam>
        /// <typeparam name="TArg12">The scalar type of parameter 12.</typeparam>
        /// <typeparam name="TArg13">The scalar type of parameter 13.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TArg9">The scalar type of parameter 9.</typeparam>
        /// <typeparam name="TArg10">The scalar type of parameter 10.</typeparam>
        /// <typeparam name="TArg11">The scalar type of parameter 11.</typeparam>
        /// <typeparam name="TArg12">The scalar type of parameter 12.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TArg9">The scalar type of parameter 9.</typeparam>
        /// <typeparam name="TArg10">The scalar type of parameter 10.</typeparam>
        /// <typeparam name="TArg11">The scalar type of parameter 11.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TArg9">The scalar type of parameter 9.</typeparam>
        /// <typeparam name="TArg10">The scalar type of parameter 10.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TArg9">The scalar type of parameter 9.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TArg8">The scalar type of parameter 8.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TArg7">The scalar type of parameter 7.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TArg6">The scalar type of parameter 6.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TArg5">The scalar type of parameter 5.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TArg4">The scalar type of parameter 4.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TArg4, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TArg4, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TArg3">The scalar type of parameter 3.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification="required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TArg3, TResult> Compile<TArg0, TArg1, TArg2, TArg3, TResult>(Expression<Func<TArg0, TArg1, TArg2, TArg3, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TArg3, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TArg2">The scalar type of parameter 2.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TArg2, TResult> Compile<TArg0, TArg1, TArg2, TResult>(Expression<Func<TArg0, TArg1, TArg2, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TArg2, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TArg1">The scalar type of parameter 1.</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TArg1, TResult> Compile<TArg0, TArg1, TResult>(Expression<Func<TArg0, TArg1, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TArg1, TResult>;
        }

        /// <summary>
        /// Creates a CompiledQuery delegate from an ELinq expression.
        /// </summary>
        /// <typeparam name="TArg0">An ObjectContext derived type</typeparam>
        /// <typeparam name="TResult">The return type of the delegate.</typeparam>
        /// <param name="query">The lambda expression to compile.</param>
        /// <returns>The CompiledQuery delegate.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "required for this feature")]
        public static Func<TArg0, TResult> Compile<TArg0, TResult>(Expression<Func<TArg0, TResult>> query) where TArg0 : ObjectContext
        {
            return new CompiledQuery(query).Invoke<TArg0, TResult>;
        }

        private TResult Invoke<TArg0, TResult>(TArg0 arg0) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0);
        }

        private TResult Invoke<TArg0, TArg1, TResult>(TArg0 arg0, TArg1 arg1) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1);
        }
        
        private TResult Invoke<TArg0, TArg1, TArg2, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12, TArg13 arg13) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12, TArg13 arg13, TArg14 arg14) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
        }

        private TResult Invoke<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TResult>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12, TArg13 arg13, TArg14 arg14, TArg15 arg15) where TArg0 : ObjectContext
        {
            EntityUtil.CheckArgumentNull(arg0, "arg0");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // This method must ensure that the O-Space metadata for TResultType is correctly loaded - it is the equivalent
            // of a public constructor for compiled queries, since it is returned as a delegate and called as a public entry point.
            arg0.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResult), System.Reflection.Assembly.GetCallingAssembly());

            return ExecuteQuery<TResult>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
        }

        private TResult ExecuteQuery<TResult>(ObjectContext context, params object[] parameterValues)
        {
            bool isSingleton;
            Type elementType = GetElementType(typeof(TResult), out isSingleton);
            ObjectQueryState queryState = new CompiledELinqQueryState(elementType, context, _query, _cacheToken, parameterValues);
            System.Collections.IEnumerable query = queryState.CreateQuery();
            if (isSingleton)
            {
                return ObjectQueryProvider.ExecuteSingle<TResult>(Enumerable.Cast<TResult>(query), _query);
            }
            else
            {
                return (TResult)query;
            }
        }

        /// <summary>
        /// This method is trying to distinguish between a set of types and a singleton type
        /// It also has the restriction that to be a set of types, it must be assignable from ObjectQuery&lt;T&gt;
        /// Otherwise we won't be able to cast our query to the set requested.
        /// </summary>
        /// <param name="resultType">The type asked for as a result type.</param>
        /// <param name="isSingleton">Is it a set of a type.</param>
        /// <returns>The element type to use</returns>
        private static Type GetElementType(Type resultType, out bool isSingleton)
        {
            Type elementType = TypeSystem.GetElementType(resultType);
            
            isSingleton = (elementType == resultType ||
                           !resultType.IsAssignableFrom(typeof(ObjectQuery<>).MakeGenericType(elementType)));

            if (isSingleton)
            {
                return resultType;
            }
            else
            {
                return elementType;
            }
        }
    }
}