forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinqBridge-1.3.cs
More file actions
executable file
·3102 lines (2516 loc) · 104 KB
/
LinqBridge-1.3.cs
File metadata and controls
executable file
·3102 lines (2516 loc) · 104 KB
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region License, Terms and Author(s)
//
// LINQBridge
// Copyright (c) 2007 Atif Aziz, Joseph Albahari. All rights reserved.
//
// Author(s):
//
// Atif Aziz, http://www.raboof.com
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the New BSD License, a copy of which should have
// been delivered along with this distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#endregion
// $Id: Enumerable.cs c08984d432b1 2012/04/17 16:05:19 azizatif $
#if !NET35
namespace System.Linq
{
#region Imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using LinqBridge;
#endregion
/// <summary>
/// Provides a set of static (Shared in Visual Basic) methods for
/// querying objects that implement <see cref="IEnumerable{T}" />.
/// </summary>
static partial class Enumerable
{
/// <summary>
/// Returns the input typed as <see cref="IEnumerable{T}"/>.
/// </summary>
public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
{
return source;
}
/// <summary>
/// Returns an empty <see cref="IEnumerable{T}"/> that has the
/// specified type argument.
/// </summary>
public static IEnumerable<TResult> Empty<TResult>()
{
return Sequence<TResult>.Empty;
}
/// <summary>
/// Converts the elements of an <see cref="IEnumerable"/> to the
/// specified type.
/// </summary>
public static IEnumerable<TResult> Cast<TResult>(
this IEnumerable source)
{
if (source == null) throw new ArgumentNullException("source");
return CastYield<TResult>(source);
}
private static IEnumerable<TResult> CastYield<TResult>(
IEnumerable source)
{
foreach (var item in source)
yield return (TResult) item;
}
/// <summary>
/// Filters the elements of an <see cref="IEnumerable"/> based on a specified type.
/// </summary>
public static IEnumerable<TResult> OfType<TResult>(
this IEnumerable source)
{
if (source == null) throw new ArgumentNullException("source");
return OfTypeYield<TResult>(source);
}
private static IEnumerable<TResult> OfTypeYield<TResult>(
IEnumerable source)
{
foreach (var item in source)
if (item is TResult)
yield return (TResult) item;
}
/// <summary>
/// Generates a sequence of integral numbers within a specified range.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="count">The number of sequential integers to generate.</param>
public static IEnumerable<int> Range(int start, int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException("count", count, null);
var end = (long) start + count;
if (end - 1 >= int.MaxValue)
throw new ArgumentOutOfRangeException("count", count, null);
return RangeYield(start, end);
}
private static IEnumerable<int> RangeYield(int start, long end)
{
for (var i = start; i < end; i++)
yield return i;
}
/// <summary>
/// Generates a sequence that contains one repeated value.
/// </summary>
public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
{
if (count < 0) throw new ArgumentOutOfRangeException("count", count, null);
return RepeatYield(element, count);
}
private static IEnumerable<TResult> RepeatYield<TResult>(TResult element, int count)
{
for (var i = 0; i < count; i++)
yield return element;
}
/// <summary>
/// Filters a sequence of values based on a predicate.
/// </summary>
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
if (predicate == null) throw new ArgumentNullException("predicate");
return source.Where((item, i) => predicate(item));
}
/// <summary>
/// Filters a sequence of values based on a predicate.
/// Each element's index is used in the logic of the predicate function.
/// </summary>
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
return WhereYield(source, predicate);
}
private static IEnumerable<TSource> WhereYield<TSource>(
IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
{
var i = 0;
foreach (var item in source)
if (predicate(item, i++))
yield return item;
}
/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector)
{
if (selector == null) throw new ArgumentNullException("selector");
return source.Select((item, i) => selector(item));
}
/// <summary>
/// Projects each element of a sequence into a new form by
/// incorporating the element's index.
/// </summary>
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, int, TResult> selector)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
return SelectYield(source, selector);
}
private static IEnumerable<TResult> SelectYield<TSource, TResult>(
IEnumerable<TSource> source,
Func<TSource, int, TResult> selector)
{
var i = 0;
foreach (var item in source)
yield return selector(item, i++);
}
/// <summary>
/// Projects each element of a sequence to an <see cref="IEnumerable{T}" />
/// and flattens the resulting sequences into one sequence.
/// </summary>
public static IEnumerable<TResult> SelectMany<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TResult>> selector)
{
if (selector == null) throw new ArgumentNullException("selector");
return source.SelectMany((item, i) => selector(item));
}
/// <summary>
/// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
/// and flattens the resulting sequences into one sequence. The
/// index of each source element is used in the projected form of
/// that element.
/// </summary>
public static IEnumerable<TResult> SelectMany<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, int, IEnumerable<TResult>> selector)
{
if (selector == null) throw new ArgumentNullException("selector");
return source.SelectMany(selector, (item, subitem) => subitem);
}
/// <summary>
/// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
/// flattens the resulting sequences into one sequence, and invokes
/// a result selector function on each element therein.
/// </summary>
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector)
{
if (collectionSelector == null) throw new ArgumentNullException("collectionSelector");
return source.SelectMany((item, i) => collectionSelector(item), resultSelector);
}
/// <summary>
/// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
/// flattens the resulting sequences into one sequence, and invokes
/// a result selector function on each element therein. The index of
/// each source element is used in the intermediate projected form
/// of that element.
/// </summary>
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
this IEnumerable<TSource> source,
Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector)
{
if (source == null) throw new ArgumentNullException("source");
if (collectionSelector == null) throw new ArgumentNullException("collectionSelector");
if (resultSelector == null) throw new ArgumentNullException("resultSelector");
return SelectManyYield(source, collectionSelector, resultSelector);
}
private static IEnumerable<TResult> SelectManyYield<TSource, TCollection, TResult>(
this IEnumerable<TSource> source,
Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector)
{
var i = 0;
foreach (var item in source)
foreach (var subitem in collectionSelector(item, i++))
yield return resultSelector(item, subitem);
}
/// <summary>
/// Returns elements from a sequence as long as a specified condition is true.
/// </summary>
public static IEnumerable<TSource> TakeWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
if (predicate == null) throw new ArgumentNullException("predicate");
return source.TakeWhile((item, i) => predicate(item));
}
/// <summary>
/// Returns elements from a sequence as long as a specified condition is true.
/// The element's index is used in the logic of the predicate function.
/// </summary>
public static IEnumerable<TSource> TakeWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
return TakeWhileYield(source, predicate);
}
private static IEnumerable<TSource> TakeWhileYield<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
{
var i = 0;
foreach (var item in source)
if (predicate(item, i++))
yield return item;
else
break;
}
/// <summary>
/// Returns a specified number of contiguous elements from the start
/// of a sequence.
/// </summary>
public static IEnumerable<TSource> Take<TSource>(
this IEnumerable<TSource> source,
int count)
{
return source.TakeWhile((item, i) => i < count);
}
private static class Futures<T>
{
public static readonly Func<T> Default = () => default(T);
public static readonly Func<T> Undefined = () => { throw new InvalidOperationException(); };
}
/// <summary>
/// Base implementation of First operator.
/// </summary>
private static TSource FirstImpl<TSource>(
this IEnumerable<TSource> source,
Func<TSource> empty)
{
if (source == null) throw new ArgumentNullException("source");
Debug.Assert(empty != null);
var list = source as IList<TSource>; // optimized case for lists
if (list != null)
return list.Count > 0 ? list[0] : empty();
using (var e = source.GetEnumerator()) // fallback for enumeration
return e.MoveNext() ? e.Current : empty();
}
/// <summary>
/// Returns the first element of a sequence.
/// </summary>
public static TSource First<TSource>(
this IEnumerable<TSource> source)
{
return source.FirstImpl(Futures<TSource>.Undefined);
}
/// <summary>
/// Returns the first element in a sequence that satisfies a specified condition.
/// </summary>
public static TSource First<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return First(source.Where(predicate));
}
/// <summary>
/// Returns the first element of a sequence, or a default value if
/// the sequence contains no elements.
/// </summary>
public static TSource FirstOrDefault<TSource>(
this IEnumerable<TSource> source)
{
return source.FirstImpl(Futures<TSource>.Default);
}
/// <summary>
/// Returns the first element of the sequence that satisfies a
/// condition or a default value if no such element is found.
/// </summary>
public static TSource FirstOrDefault<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return FirstOrDefault(source.Where(predicate));
}
/// <summary>
/// Base implementation of Last operator.
/// </summary>
private static TSource LastImpl<TSource>(
this IEnumerable<TSource> source,
Func<TSource> empty)
{
if (source == null) throw new ArgumentNullException("source");
var list = source as IList<TSource>; // optimized case for lists
if (list != null)
return list.Count > 0 ? list[list.Count - 1] : empty();
using (var e = source.GetEnumerator())
{
if (!e.MoveNext())
return empty();
var last = e.Current;
while (e.MoveNext())
last = e.Current;
return last;
}
}
/// <summary>
/// Returns the last element of a sequence.
/// </summary>
public static TSource Last<TSource>(
this IEnumerable<TSource> source)
{
return source.LastImpl(Futures<TSource>.Undefined);
}
/// <summary>
/// Returns the last element of a sequence that satisfies a
/// specified condition.
/// </summary>
public static TSource Last<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return Last(source.Where(predicate));
}
/// <summary>
/// Returns the last element of a sequence, or a default value if
/// the sequence contains no elements.
/// </summary>
public static TSource LastOrDefault<TSource>(
this IEnumerable<TSource> source)
{
return source.LastImpl(Futures<TSource>.Default);
}
/// <summary>
/// Returns the last element of a sequence that satisfies a
/// condition or a default value if no such element is found.
/// </summary>
public static TSource LastOrDefault<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return LastOrDefault(source.Where(predicate));
}
/// <summary>
/// Base implementation of Single operator.
/// </summary>
private static TSource SingleImpl<TSource>(
this IEnumerable<TSource> source,
Func<TSource> empty)
{
if (source == null) throw new ArgumentNullException("source");
using (var e = source.GetEnumerator())
{
if (e.MoveNext())
{
var single = e.Current;
if (!e.MoveNext())
return single;
throw new InvalidOperationException();
}
return empty();
}
}
/// <summary>
/// Returns the only element of a sequence, and throws an exception
/// if there is not exactly one element in the sequence.
/// </summary>
public static TSource Single<TSource>(
this IEnumerable<TSource> source)
{
return source.SingleImpl(Futures<TSource>.Undefined);
}
/// <summary>
/// Returns the only element of a sequence that satisfies a
/// specified condition, and throws an exception if more than one
/// such element exists.
/// </summary>
public static TSource Single<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return Single(source.Where(predicate));
}
/// <summary>
/// Returns the only element of a sequence, or a default value if
/// the sequence is empty; this method throws an exception if there
/// is more than one element in the sequence.
/// </summary>
public static TSource SingleOrDefault<TSource>(
this IEnumerable<TSource> source)
{
return source.SingleImpl(Futures<TSource>.Default);
}
/// <summary>
/// Returns the only element of a sequence that satisfies a
/// specified condition or a default value if no such element
/// exists; this method throws an exception if more than one element
/// satisfies the condition.
/// </summary>
public static TSource SingleOrDefault<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return SingleOrDefault(source.Where(predicate));
}
/// <summary>
/// Returns the element at a specified index in a sequence.
/// </summary>
public static TSource ElementAt<TSource>(
this IEnumerable<TSource> source,
int index)
{
if (source == null) throw new ArgumentNullException("source");
if (index < 0)
throw new ArgumentOutOfRangeException("index", index, null);
var list = source as IList<TSource>;
if (list != null)
return list[index];
try
{
return source.SkipWhile((item, i) => i < index).First();
}
catch (InvalidOperationException) // if thrown by First
{
throw new ArgumentOutOfRangeException("index", index, null);
}
}
/// <summary>
/// Returns the element at a specified index in a sequence or a
/// default value if the index is out of range.
/// </summary>
public static TSource ElementAtOrDefault<TSource>(
this IEnumerable<TSource> source,
int index)
{
if (source == null) throw new ArgumentNullException("source");
if (index < 0)
return default(TSource);
var list = source as IList<TSource>;
if (list != null)
return index < list.Count ? list[index] : default(TSource);
return source.SkipWhile((item, i) => i < index).FirstOrDefault();
}
/// <summary>
/// Inverts the order of the elements in a sequence.
/// </summary>
public static IEnumerable<TSource> Reverse<TSource>(
this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException("source");
return ReverseYield(source);
}
private static IEnumerable<TSource> ReverseYield<TSource>(IEnumerable<TSource> source)
{
var stack = new Stack<TSource>();
foreach (var item in source)
stack.Push(item);
foreach (var item in stack)
yield return item;
}
/// <summary>
/// Bypasses elements in a sequence as long as a specified condition
/// is true and then returns the remaining elements.
/// </summary>
public static IEnumerable<TSource> SkipWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
if (predicate == null) throw new ArgumentNullException("predicate");
return source.SkipWhile((item, i) => predicate(item));
}
/// <summary>
/// Bypasses elements in a sequence as long as a specified condition
/// is true and then returns the remaining elements. The element's
/// index is used in the logic of the predicate function.
/// </summary>
public static IEnumerable<TSource> SkipWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
return SkipWhileYield(source, predicate);
}
private static IEnumerable<TSource> SkipWhileYield<TSource>(
IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
{
using (var e = source.GetEnumerator())
{
for (var i = 0; ; i++)
{
if (!e.MoveNext())
yield break;
if (!predicate(e.Current, i))
break;
}
do { yield return e.Current; } while (e.MoveNext());
}
}
/// <summary>
/// Bypasses a specified number of elements in a sequence and then
/// returns the remaining elements.
/// </summary>
public static IEnumerable<TSource> Skip<TSource>(
this IEnumerable<TSource> source,
int count)
{
return source.SkipWhile((item, i) => i < count);
}
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
public static int Count<TSource>(
this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException("source");
var collection = source as ICollection;
return collection != null
? collection.Count
: source.Aggregate(0, (count, item) => checked(count + 1));
}
/// <summary>
/// Returns a number that represents how many elements in the
/// specified sequence satisfy a condition.
/// </summary>
public static int Count<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return Count(source.Where(predicate));
}
/// <summary>
/// Returns an <see cref="Int64"/> that represents the total number
/// of elements in a sequence.
/// </summary>
public static long LongCount<TSource>(
this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException("source");
var array = source as Array;
return array != null
? array.LongLength
: source.Aggregate(0L, (count, item) => count + 1);
}
/// <summary>
/// Returns an <see cref="Int64"/> that represents how many elements
/// in a sequence satisfy a condition.
/// </summary>
public static long LongCount<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return LongCount(source.Where(predicate));
}
/// <summary>
/// Concatenates two sequences.
/// </summary>
public static IEnumerable<TSource> Concat<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second)
{
if (first == null) throw new ArgumentNullException("first");
if (second == null) throw new ArgumentNullException("second");
return ConcatYield(first, second);
}
private static IEnumerable<TSource> ConcatYield<TSource>(
IEnumerable<TSource> first,
IEnumerable<TSource> second)
{
foreach (var item in first)
yield return item;
foreach (var item in second)
yield return item;
}
/// <summary>
/// Creates a <see cref="List{T}"/> from an <see cref="IEnumerable{T}"/>.
/// </summary>
public static List<TSource> ToList<TSource>(
this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException("source");
return new List<TSource>(source);
}
/// <summary>
/// Creates an array from an <see cref="IEnumerable{T}"/>.
/// </summary>
public static TSource[] ToArray<TSource>(
this IEnumerable<TSource> source)
{
return source.ToList().ToArray();
}
/// <summary>
/// Returns distinct elements from a sequence by using the default
/// equality comparer to compare values.
/// </summary>
public static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source)
{
return Distinct(source, /* comparer */ null);
}
/// <summary>
/// Returns distinct elements from a sequence by using a specified
/// <see cref="IEqualityComparer{T}"/> to compare values.
/// </summary>
public static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source,
IEqualityComparer<TSource> comparer)
{
if (source == null) throw new ArgumentNullException("source");
return DistinctYield(source, comparer);
}
private static IEnumerable<TSource> DistinctYield<TSource>(
IEnumerable<TSource> source,
IEqualityComparer<TSource> comparer)
{
var set = new Dictionary<TSource, object>(comparer);
var gotNull = false;
foreach (var item in source)
{
if (item == null)
{
if (gotNull)
continue;
gotNull = true;
}
else
{
if (set.ContainsKey(item))
continue;
set.Add(item, null);
}
yield return item;
}
}
/// <summary>
/// Creates a <see cref="Lookup{TKey,TElement}" /> from an
/// <see cref="IEnumerable{T}" /> according to a specified key
/// selector function.
/// </summary>
public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
return ToLookup(source, keySelector, e => e, /* comparer */ null);
}
/// <summary>
/// Creates a <see cref="Lookup{TKey,TElement}" /> from an
/// <see cref="IEnumerable{T}" /> according to a specified key
/// selector function and a key comparer.
/// </summary>
public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer)
{
return ToLookup(source, keySelector, e => e, comparer);
}
/// <summary>
/// Creates a <see cref="Lookup{TKey,TElement}" /> from an
/// <see cref="IEnumerable{T}" /> according to specified key
/// and element selector functions.
/// </summary>
public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector)
{
return ToLookup(source, keySelector, elementSelector, /* comparer */ null);
}
/// <summary>
/// Creates a <see cref="Lookup{TKey,TElement}" /> from an
/// <see cref="IEnumerable{T}" /> according to a specified key
/// selector function, a comparer and an element selector function.
/// </summary>
public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector,
IEqualityComparer<TKey> comparer)
{
if (source == null) throw new ArgumentNullException("source");
if (keySelector == null) throw new ArgumentNullException("keySelector");
if (elementSelector == null) throw new ArgumentNullException("elementSelector");
var lookup = new Lookup<TKey, TElement>(comparer);
foreach (var item in source)
{
var key = keySelector(item);
var grouping = (Grouping<TKey, TElement>) lookup.Find(key);
if (grouping == null)
{
grouping = new Grouping<TKey, TElement>(key);
lookup.Add(grouping);
}
grouping.Add(elementSelector(item));
}
return lookup;
}
/// <summary>
/// Groups the elements of a sequence according to a specified key
/// selector function.
/// </summary>
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
return GroupBy(source, keySelector, /* comparer */ null);
}
/// <summary>
/// Groups the elements of a sequence according to a specified key
/// selector function and compares the keys by using a specified
/// comparer.
/// </summary>
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer)
{
return GroupBy(source, keySelector, e => e, comparer);
}
/// <summary>
/// Groups the elements of a sequence according to a specified key
/// selector function and projects the elements for each group by
/// using a specified function.
/// </summary>
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector)
{
return GroupBy(source, keySelector, elementSelector, /* comparer */ null);
}
/// <summary>
/// Groups the elements of a sequence according to a specified key
/// selector function and creates a result value from each group and
/// its key.
/// </summary>
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector,
IEqualityComparer<TKey> comparer)
{
if (source == null) throw new ArgumentNullException("source");
if (keySelector == null) throw new ArgumentNullException("keySelector");
if (elementSelector == null) throw new ArgumentNullException("elementSelector");
return ToLookup(source, keySelector, elementSelector, comparer);
}
/// <summary>
/// Groups the elements of a sequence according to a key selector
/// function. The keys are compared by using a comparer and each
/// group's elements are projected by using a specified function.
/// </summary>
public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
{
return GroupBy(source, keySelector, resultSelector, /* comparer */ null);
}