6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
8 // Copyright (c) 2010 Jérémie "Garuma" Laval
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
35 internal class QuerySelectManyNode<TSource, TCollection, TResult> : QueryStreamNode<TResult, TSource>
37 Func<TSource, IEnumerable<TCollection>> collectionSelector;
38 Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed;
39 Func<TSource, TCollection, TResult> resultSelector;
41 internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
42 Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed,
43 Func<TSource, TCollection, TResult> resultSelector)
46 this.collectionSelectorIndexed = collectionSelectorIndexed;
47 this.resultSelector = resultSelector;
50 internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
51 Func<TSource, IEnumerable<TCollection>> collectionSelector,
52 Func<TSource, TCollection, TResult> resultSelector)
53 : base (parent, false)
55 this.collectionSelector = collectionSelector;
56 this.resultSelector = resultSelector;
59 internal override IEnumerable<TResult> GetSequential ()
61 IEnumerable<TSource> source = Parent.GetSequential ();
64 return source.SelectMany (collectionSelectorIndexed, resultSelector);
66 return source.SelectMany (collectionSelector, resultSelector);
69 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
71 IEnumerable<TResult>[] result = null;
74 IList<IEnumerable<KeyValuePair<long, TSource>>> enumerables = Parent.GetOrderedEnumerables (options);
75 result = new IEnumerable<TResult>[enumerables.Count];
77 for (int i = 0; i < enumerables.Count; i++)
78 result[i] = GetEnumerableInternal<KeyValuePair<long, TSource>> (enumerables[i],
79 (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
80 (e, c) => resultSelector (e.Value, c));
82 IList<IEnumerable<TSource>> enumerables = Parent.GetEnumerables (options);
83 result = new IEnumerable<TResult>[enumerables.Count];
85 for (int i = 0; i < enumerables.Count; i++)
86 result[i] = GetEnumerableInternal<TSource> (enumerables[i],
88 (e, c) => resultSelector (e, c));
94 // This one is gonna be tricky
95 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
97 throw new NotImplementedException ();
100 IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
101 Func<T, IEnumerable<TCollection>> collectionner,
102 Func<T, TCollection, TResult> packer)
104 foreach (T element in source)
105 foreach (TCollection item in collectionner (element))
106 yield return packer (element, item);
109 IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
112 foreach (KeyValuePair<long, TSource> element in source) {
113 IEnumerable<TCollection> collection = collectionSelectorIndexed (element.Value, (int)element.Key);
115 foreach (TCollection item in collection)
116 yield return new KeyValuePair<long, TResult> (-1, resultSelector (element.Value, item));