Merge pull request #487 from mayerwin/patch-1
[mono.git] / mcs / class / System.Core / System.Linq / Queryable.cs
index bdea884d7d2080bff2c83c6953d1aaf29621b189..e9b98cca9e1e536efeaa264195820cc503e3654f 100644 (file)
@@ -77,7 +77,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate)),
                                        source.Expression,
-                                       Expression.Constant (seed),
+                                       Expression.Constant (seed, typeof (TAccumulate)),
                                        Expression.Quote (func)));
                }
 
@@ -88,7 +88,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate), typeof (TResult)),
                                        source.Expression,
-                                       Expression.Constant (seed),
+                                       Expression.Constant (seed, typeof (TAccumulate)),
                                        Expression.Quote (func),
                                        Expression.Quote (selector)));
                }
@@ -139,22 +139,31 @@ namespace System.Linq {
 
                public static IQueryable<TElement> AsQueryable<TElement> (this IEnumerable<TElement> source)
                {
+                       if (source == null)
+                               throw new ArgumentNullException ("source");
+
                        var queryable = source as IQueryable<TElement>;
                        if (queryable != null)
                                return queryable;
 
-                       return new QueryableEnumerable<TElement> (
-                               new ConstantExpression (source, typeof (IQueryable<TElement>)));
+                       return new QueryableEnumerable<TElement> (source);
                }
 
-               [MonoTODO]
                public static IQueryable AsQueryable (this IEnumerable source)
                {
+                       if (source == null)
+                               throw new ArgumentNullException ("source");
+
                        var queryable = source as IQueryable;
                        if (queryable != null)
                                return queryable;
 
-                       throw new NotImplementedException ();
+                       Type ienumerable;
+                       if (!source.GetType ().IsGenericImplementationOf (typeof (IEnumerable<>), out ienumerable))
+                               throw new ArgumentException ("source is not IEnumerable<>");
+
+                       return (IQueryable) Activator.CreateInstance (
+                               typeof (QueryableEnumerable<>).MakeGenericType (ienumerable.GetFirstGenericArgument ()), source);
                }
 
                #endregion
@@ -396,7 +405,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>))));
                }
 
                #endregion
@@ -411,7 +420,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source.Expression,
-                                       Expression.Constant (item)));
+                                       Expression.Constant (item, typeof (TSource))));
                }
 
                public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer)
@@ -422,8 +431,8 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source.Expression,
-                                       Expression.Constant (item),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (item, typeof (TSource)),
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TSource>))));
                }
 
                #endregion
@@ -470,7 +479,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source.Expression,
-                                       Expression.Constant (defaultValue)));
+                                       Expression.Constant (defaultValue, typeof (TSource))));
                }
 
                #endregion
@@ -495,7 +504,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source.Expression,
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TSource>))));
                }
 
                #endregion
@@ -540,7 +549,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>))));
                }
 
                public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
@@ -551,8 +560,8 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>)),
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TSource>))));
                }
 
                #endregion
@@ -628,7 +637,7 @@ namespace System.Linq {
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
                                        source.Expression,
                                        Expression.Quote (keySelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TKey>))));
                }
                public static IQueryable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
                {
@@ -662,7 +671,7 @@ namespace System.Linq {
                                        source.Expression,
                                        Expression.Quote (keySelector),
                                        Expression.Quote (elementSelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TKey>))));
                }
                public static IQueryable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>, TResult>> resultSelector)
                {
@@ -687,7 +696,7 @@ namespace System.Linq {
                                        source.Expression,
                                        Expression.Quote (keySelector),
                                        Expression.Quote (resultSelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TKey>))));
                }
                public static IQueryable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
                {
@@ -700,7 +709,7 @@ namespace System.Linq {
                                        Expression.Quote (keySelector),
                                        Expression.Quote (elementSelector),
                                        Expression.Quote (resultSelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TKey>))));
                }
                #endregion
 
@@ -723,7 +732,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
                                        outer.Expression,
-                                       Expression.Constant (inner),
+                                       Expression.Constant (inner, typeof (IEnumerable<TInner>)),
                                        Expression.Quote (outerKeySelector),
                                        Expression.Quote (innerKeySelector),
                                        Expression.Quote (resultSelector)));
@@ -746,7 +755,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
                                        outer.Expression,
-                                       Expression.Constant (inner),
+                                       Expression.Constant (inner, typeof (IEnumerable<TInner>)),
                                        Expression.Quote (outerKeySelector),
                                        Expression.Quote (innerKeySelector),
                                        Expression.Quote (resultSelector),
@@ -765,7 +774,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>))));
                }
 
                public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
@@ -776,8 +785,8 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>)),
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TSource>))));
                }
 
                #endregion
@@ -792,7 +801,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
                                        outer.Expression,
-                                       Expression.Constant (inner),
+                                       Expression.Constant (inner, typeof (IEnumerable<TInner>)),
                                        Expression.Quote (outerKeySelector),
                                        Expression.Quote (innerKeySelector),
                                        Expression.Quote (resultSelector)));
@@ -806,11 +815,11 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
                                        outer.Expression,
-                                       Expression.Constant (inner),
+                                       Expression.Constant (inner, typeof (IEnumerable<TInner>)),
                                        Expression.Quote (outerKeySelector),
                                        Expression.Quote (innerKeySelector),
                                        Expression.Quote (resultSelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TKey>))));
                }
 
 
@@ -976,7 +985,7 @@ namespace System.Linq {
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
                                        source.Expression,
                                        Expression.Quote (keySelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IComparer<TKey>))));
                }
 
                #endregion
@@ -1003,7 +1012,7 @@ namespace System.Linq {
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
                                        source.Expression,
                                        Expression.Quote (keySelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IComparer<TKey>))));
                }
 
                #endregion
@@ -1108,7 +1117,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>))));
                }
 
                public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
@@ -1119,8 +1128,8 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>)),
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TSource>))));
                }
 
                #endregion
@@ -1507,7 +1516,7 @@ namespace System.Linq {
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
                                        source.Expression,
                                        Expression.Quote (keySelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IComparer<TKey>))));
                }
 
                #endregion
@@ -1534,7 +1543,7 @@ namespace System.Linq {
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
                                        source.Expression,
                                        Expression.Quote (keySelector),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (comparer, typeof (IComparer<TKey>))));
                }
 
                #endregion
@@ -1549,7 +1558,7 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>))));
                }
 
                public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
@@ -1560,8 +1569,8 @@ namespace System.Linq {
                                StaticCall (
                                        MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
                                        source1.Expression,
-                                       Expression.Constant (source2),
-                                       Expression.Constant (comparer)));
+                                       Expression.Constant (source2, typeof (IEnumerable<TSource>)),
+                                       Expression.Constant (comparer, typeof (IEqualityComparer<TSource>))));
                }
 
 
@@ -1594,5 +1603,23 @@ namespace System.Linq {
 
                #endregion
 
+#if NET_4_0 || MOONLIGHT || MOBILE
+               #region Zip
+
+               public static IQueryable<TResult> Zip<TFirst, TSecond, TResult> (this IQueryable<TFirst> source1, IEnumerable<TSecond> source2, Expression<Func<TFirst, TSecond, TResult>> resultSelector)
+               {
+                       Check.Source1AndSource2 (source1, source2);
+                       if (resultSelector == null)
+                               throw new ArgumentNullException ("resultSelector");
+
+                       return source1.Provider.CreateQuery<TResult> (
+                               StaticCall (
+                                       MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TFirst), typeof (TSecond), typeof (TResult)),
+                                       source1.Expression,
+                                       Expression.Quote (resultSelector)));
+               }
+
+               #endregion
+#endif
        }
 }