Skip to content

Commit

Permalink
Allow custom count query
Browse files Browse the repository at this point in the history
  • Loading branch information
mcintyre321 committed Sep 5, 2014
1 parent ee86808 commit 8ef888e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
10 changes: 10 additions & 0 deletions LinqToAnything.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ public void CanHandleAProjectionASkipAndAnOrderByAsc()
var pq = new DelegateQueryable<SomeEntity>(getPageFromDataSource);
pq.OrderBy(e => e.Name).ToArray();
}
[Test]
public void CanDoAnOptimizedCount()
{
DataQuery<SomeEntity> getPageFromDataSource = (info) =>
{
throw new NotImplementedException();
};
var pq = new DelegateQueryable<SomeEntity>(getPageFromDataSource, qi => 15);
Assert.AreEqual(15, pq.Count(x => x.Index > 1));
}



Expand Down
1 change: 1 addition & 0 deletions LinqToAnything/DataQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
namespace LinqToAnything
{
public delegate IEnumerable<T> DataQuery<out T>(QueryInfo info);
public delegate int CountQuery(QueryInfo info);
}
9 changes: 5 additions & 4 deletions LinqToAnything/DelegateQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ public class DelegateQueryable<T> : IOrderedQueryable<T>
QueryProvider<T> provider;
Expression expression;

public DelegateQueryable(DataQuery<T> dataQuery)
public DelegateQueryable(DataQuery<T> dataQuery, CountQuery countQuery = null)
{
this.provider = new QueryProvider<T>(dataQuery);

this.provider = new QueryProvider<T>(dataQuery, countQuery ?? (qi => dataQuery(qi).Count()));
this.expression = Expression.Constant(this);
}

public DelegateQueryable(DataQuery<T> dataQuery, Expression expression, QueryVisitor ev)
internal DelegateQueryable(DataQuery<T> dataQuery, CountQuery countQuery, Expression expression, QueryVisitor ev)
{

this.provider = new QueryProvider<T>(dataQuery, ev);
this.provider = new QueryProvider<T>(dataQuery, countQuery, ev);
this.expression = expression ?? Expression.Constant(this);

}
Expand Down
19 changes: 0 additions & 19 deletions LinqToAnything/Extension.cs

This file was deleted.

1 change: 0 additions & 1 deletion LinqToAnything/LinqToAnything.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ExpressionUtils.cs" />
<Compile Include="Extension.cs" />
<Compile Include="QueryInfo.cs" />
<Compile Include="DelegateQueryable.cs" />
<Compile Include="DataQuery.cs" />
Expand Down
20 changes: 14 additions & 6 deletions LinqToAnything/QueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ namespace LinqToAnything
public class QueryProvider<T> : IQueryProvider
{
private readonly DataQuery<T> _dataQuery;
private readonly CountQuery countQuery;
private QueryVisitor _queryVisitor;


public QueryProvider(DataQuery<T> dataQuery, QueryVisitor _queryVisitor = null)
public QueryProvider(DataQuery<T> dataQuery, CountQuery countQuery, QueryVisitor queryVisitor = null)
{
_dataQuery = dataQuery;
this._queryVisitor = _queryVisitor ?? new QueryVisitor();
this.countQuery = countQuery;
this._queryVisitor = queryVisitor ?? new QueryVisitor();
}

public IQueryable CreateQuery(Expression expression)
Expand All @@ -30,9 +32,9 @@ public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
if (typeof(TElement) != typeof(T))
{
DataQuery<TElement> q = info => _dataQuery(info).Select(queryVisitor.Transform<T, TElement>());
return new DelegateQueryable<TElement>(q, null, queryVisitor);
return new DelegateQueryable<TElement>(q, countQuery, null, queryVisitor);
}
return new DelegateQueryable<TElement>((DataQuery<TElement>)((object)_dataQuery), expression, queryVisitor);
return new DelegateQueryable<TElement>((DataQuery<TElement>)((object)_dataQuery), countQuery, expression, queryVisitor);

}

Expand All @@ -41,9 +43,9 @@ public IEnumerable<TResult> GetEnumerable<TResult>()
{
var queryVisitor = new QueryVisitor(_queryVisitor.QueryInfo.Clone());
var results = _dataQuery(queryVisitor.QueryInfo);
//if (queryVisitor.Select != null)
//if (countQuery.Select != null)
//{
// var projectionFunc = (Func<T, TResult>)queryVisitor.Select.Lambda.Compile();
// var projectionFunc = (Func<T, TResult>)countQuery.Select.Lambda.Compile();
// return results.Select(projectionFunc);
//}
return (IEnumerable<TResult>) results;
Expand All @@ -57,8 +59,14 @@ object IQueryProvider.Execute(Expression expression)
public TResult Execute<TResult>(Expression expression)
{
var methodCallExpression = (MethodCallExpression)expression;

var queryVisitor = new QueryVisitor(_queryVisitor.QueryInfo.Clone());
queryVisitor.Visit(expression);
if (methodCallExpression.Method.Name == "Count" && typeof(TResult) == typeof(int))
{
return (TResult) (object) countQuery(queryVisitor.QueryInfo);
}

var array = _dataQuery(queryVisitor.QueryInfo).ToList();
var data = array.AsQueryable();

Expand Down

0 comments on commit 8ef888e

Please sign in to comment.