diff --git a/src/Libraries/IQToolkit/IQToolkit.dll b/src/Libraries/IQToolkit/IQToolkit.dll
new file mode 100644
index 0000000000..f2a3fdc532
Binary files /dev/null and b/src/Libraries/IQToolkit/IQToolkit.dll differ
diff --git a/src/Libraries/IQToolkit/IQToolkit/IQToolkit.csproj b/src/Libraries/IQToolkit/IQToolkit/IQToolkit.csproj
new file mode 100644
index 0000000000..bd53f3680a
--- /dev/null
+++ b/src/Libraries/IQToolkit/IQToolkit/IQToolkit.csproj
@@ -0,0 +1,64 @@
+
+
+
+ Debug
+ AnyCPU
+ 9.0.21022
+ 2.0
+ {0DA62A7C-3B40-456F-A4B2-B3E19ED4080E}
+ Library
+ Properties
+ IQToolkit
+ IQToolkit
+ v4.0
+ 512
+
+
+
+
+ true
+ full
+ false
+ bin\Debug\
+ TRACE;DEBUG
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+
+ 3.5
+
+
+ 3.0
+
+
+ 3.5
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Libraries/IQToolkit/IQToolkit/IQToolkit.sln b/src/Libraries/IQToolkit/IQToolkit/IQToolkit.sln
new file mode 100644
index 0000000000..3a2626e801
--- /dev/null
+++ b/src/Libraries/IQToolkit/IQToolkit/IQToolkit.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 10
+# SharpDevelop 4.0.0.4995
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IQToolkit", "IQToolkit.csproj", "{0DA62A7C-3B40-456F-A4B2-B3E19ED4080E}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {0DA62A7C-3B40-456F-A4B2-B3E19ED4080E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {0DA62A7C-3B40-456F-A4B2-B3E19ED4080E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {0DA62A7C-3B40-456F-A4B2-B3E19ED4080E}.Release|Any CPU.Build.0 = Release|Any CPU
+ {0DA62A7C-3B40-456F-A4B2-B3E19ED4080E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ EndGlobalSection
+EndGlobal
diff --git a/src/Libraries/IQToolkit/IQToolkit/PartialEvaluator.cs b/src/Libraries/IQToolkit/IQToolkit/PartialEvaluator.cs
new file mode 100644
index 0000000000..0a6ca73ab4
--- /dev/null
+++ b/src/Libraries/IQToolkit/IQToolkit/PartialEvaluator.cs
@@ -0,0 +1,175 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// This source code is made available under the terms of the Microsoft Public License (MS-PL)
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+
+namespace IQToolkit
+{
+ ///
+ /// Rewrites an expression tree so that locally isolatable sub-expressions are evaluated and converted into ConstantExpression nodes.
+ ///
+ public static class PartialEvaluator
+ {
+ ///
+ /// Performs evaluation & replacement of independent sub-trees
+ ///
+ /// The root of the expression tree.
+ /// A function that decides whether a given expression node can be part of the local function.
+ /// A new tree with sub-trees evaluated and replaced.
+ public static Expression Eval(Expression expression, Func fnCanBeEvaluated)
+ {
+ return SubtreeEvaluator.Eval(Nominator.Nominate(fnCanBeEvaluated, expression), expression);
+ }
+
+ ///
+ /// Performs evaluation & replacement of independent sub-trees
+ ///
+ /// The root of the expression tree.
+ /// A new tree with sub-trees evaluated and replaced.
+ public static Expression Eval(Expression expression)
+ {
+ return Eval(expression, PartialEvaluator.CanBeEvaluatedLocally);
+ }
+
+ private static bool CanBeEvaluatedLocally(Expression expression)
+ {
+ return expression.NodeType != ExpressionType.Parameter;
+ }
+
+ ///
+ /// Evaluates & replaces sub-trees when first candidate is reached (top-down)
+ ///
+ class SubtreeEvaluator : ExpressionVisitor
+ {
+ HashSet candidates;
+
+ private SubtreeEvaluator(HashSet candidates)
+ {
+ this.candidates = candidates;
+ }
+
+ internal static Expression Eval(HashSet candidates, Expression exp)
+ {
+ return new SubtreeEvaluator(candidates).Visit(exp);
+ }
+
+ public override Expression Visit(Expression exp)
+ {
+ if (exp == null)
+ {
+ return null;
+ }
+ if (this.candidates.Contains(exp))
+ {
+ return this.Evaluate(exp);
+ }
+ return base.Visit(exp);
+ }
+
+ private Expression Evaluate(Expression e)
+ {
+ Type type = e.Type;
+ if (e.NodeType == ExpressionType.Convert)
+ {
+ // check for unnecessary convert & strip them
+ var u = (UnaryExpression)e;
+ if (TypeHelper.GetNonNullableType(u.Operand.Type) == TypeHelper.GetNonNullableType(type))
+ {
+ e = ((UnaryExpression)e).Operand;
+ }
+ }
+ if (e.NodeType == ExpressionType.Constant)
+ {
+ // in case we actually threw out a nullable conversion above, simulate it here
+ if (e.Type == type)
+ {
+ return e;
+ }
+ else if (TypeHelper.GetNonNullableType(e.Type) == TypeHelper.GetNonNullableType(type))
+ {
+ return Expression.Constant(((ConstantExpression)e).Value, type);
+ }
+ }
+ var me = e as MemberExpression;
+ if (me != null)
+ {
+ // member accesses off of constant's are common, and yet since these partial evals
+ // are never re-used, using reflection to access the member is faster than compiling
+ // and invoking a lambda
+ var ce = me.Expression as ConstantExpression;
+ if (ce != null)
+ {
+ return Expression.Constant(me.Member.GetValue(ce.Value), type);
+ }
+ }
+ if (type.IsValueType)
+ {
+ e = Expression.Convert(e, typeof(object));
+ }
+ Expression> lambda = Expression.Lambda>(e);
+#if NOREFEMIT
+ Func