Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildQuery now calls IQueryProvider.CreateQuery generically, which prese... #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions LinqToQuerystring/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

using Antlr.Runtime;
using Antlr.Runtime.Tree;
Expand Down Expand Up @@ -95,7 +97,7 @@ public static object LinqToQuerystring(this IQueryable query, Type inputType, st
{
var children = tree.Children.Cast<TreeNode>().ToList();
children.Sort();

// These should always come first
foreach (var node in children.Where(o => !(o is SelectNode) && !(o is InlineCountNode)))
{
Expand Down Expand Up @@ -140,8 +142,9 @@ private static void BuildQuery(TreeNode node, ref IQueryable queryResult, ref IQ
}
else
{
queryResult = queryResult.Provider.CreateQuery(
node.BuildLinqExpression(queryResult, queryResult.Expression));
var expression = node.BuildLinqExpression(queryResult, queryResult.Expression);
var queryType = expression.Type.GetGenericArguments()[0];
queryResult = CreateQuery(queryResult.Provider, expression, queryType);
}
}

Expand All @@ -152,12 +155,18 @@ private static void BuildQuery(TreeNode node, ref IQueryable queryResult, ref IQ
}
else
{
constrainedQuery =
constrainedQuery.Provider.CreateQuery(
node.BuildLinqExpression(constrainedQuery, constrainedQuery.Expression));
var expression = node.BuildLinqExpression(constrainedQuery, constrainedQuery.Expression);
var queryType = expression.Type.GetGenericArguments()[0];
constrainedQuery = CreateQuery(constrainedQuery.Provider, expression, queryType);
}
}

private static IQueryable CreateQuery(IQueryProvider provider, Expression expression, Type queryType)
{
var genericMethod = _createQueryMethodInfo.MakeGenericMethod(queryType);
return (IQueryable)genericMethod.Invoke(provider,new object[] { expression });
}

private static IQueryable ProjectQuery(IQueryable constrainedQuery, TreeNode node)
{
// TODO: Find a solution to the following:
Expand Down Expand Up @@ -189,5 +198,7 @@ static IEnumerable Iterate(this IEnumerator iterator)
while (iterator.MoveNext())
yield return iterator.Current;
}

private static MethodInfo _createQueryMethodInfo = typeof(IQueryProvider).GetMethods().First(x => x.Name == "CreateQuery" && x.IsGenericMethod);
}
}