Skip to content

AsQueryable() on navigation property #6903

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

Closed
ErazerBrecht opened this issue Oct 30, 2016 · 3 comments
Closed

AsQueryable() on navigation property #6903

ErazerBrecht opened this issue Oct 30, 2016 · 3 comments

Comments

@ErazerBrecht
Copy link

ErazerBrecht commented Oct 30, 2016

Steps to reproduce

Use AsQueryable() on navigation property

Probably I'm doing something incorrect. Thanks in advance for reading this.

The issue

I need to check the same specific condition (where clause) many times.
I this condition changes I don't want to change all my queries. So I wanted to abstract this condition in a separate method.

return _ctx.Projects.Where(p => p.CompanyId == companyId &&
                          (p.Type == Enums.ProjectType.Open || 
                           p.Invites.Any(i => i.InviteeId == userId))).ToList()

Changed it with a extension method (on IQueryable) to:

return _ctx.Projects.Where(p => p.CompanyId == companyId)
                    .IsVisibleForResearcher(userId).ToList()

Everything working fine (generates the same SQL query). Now I also need this where projects is a navigation property:

Imagine that a company has projects. And I only want to retrieve the companies where the user can at least see one project.

return _ctx.Companies
       .Where(c => c.Projects.AsQueryable().IsVisibleForCompanyAccount(userId).Any())

After searching a while I learnt about Expression<Func<Project, bool>>

return _ctx.Companies
            .Where(c => c.Projects.AsQueryable().Any(IsProjectVisibleForResearcher(userId)))

This however resulted in the same exception.

Extra details:

Extension method:

public static IEnumerable<Project> IsVisibleForResearcher(this ICollection<Project> projects, string userId)
{
    return projects.Where(p => p.Type == Enums.ProjectType.Open || p.Invites.Any(i => i.InviteeId == userId));
}

Expression method:

public Expression<Func<Project, bool>> IsProjectVisibleForResearcher(string userId)
{
    return p => p.Type == Enums.ProjectType.Open || p.Invites.Any(i => i.InviteeId == userId);
}

Exception

Exception message: Could not parse expression 'c.Projects.AsQueryable()': This overload of the method 'System.Linq.Queryable.AsQueryable' is currently not supported.

Stack trace:
   at Remotion.Linq.Parsing.Structure.MethodCallExpressionParser.GetNodeType(MethodCallExpression expressionToParse)
   at Remotion.Linq.Parsing.Structure.MethodCallExpressionParser.Parse(String associatedIdentifier, IExpressionNode source, IEnumerable`1 arguments, MethodCallExpression expressionToParse)
   at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseMethodCallExpression(MethodCallExpression methodCallExpression, String associatedIdentifier)
   at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseMethodCallExpression(MethodCallExpression methodCallExpression, String associatedIdentifier)
   at Remotion.Linq.Parsing.Structure.QueryParser.GetParsedQuery(Expression expressionTreeRoot)
   at Remotion.Linq.Parsing.ExpressionVisitors.SubQueryFindingExpressionVisitor.Visit(Expression expression)
   at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   at Remotion.Linq.Parsing.ExpressionVisitors.SubQueryFindingExpressionVisitor.Visit(Expression expression)
   at System.Linq.Enumerable.SelectListPartitionIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Remotion.Linq.Parsing.Structure.MethodCallExpressionParser.Parse(String associatedIdentifier, IExpressionNode source, IEnumerable`1 arguments, MethodCallExpression expressionToParse)
   at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseMethodCallExpression(MethodCallExpression methodCallExpression, String associatedIdentifier)
   at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseMethodCallExpression(MethodCallExpression methodCallExpression, String associatedIdentifier)
   at Remotion.Linq.Parsing.Structure.QueryParser.GetParsedQuery(Expression expressionTreeRoot)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0`1.<CompileQuery>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at DAL.Repositories.CompanyRepository.GetAllAllowedCompanies(String userId) in E:\DAL\Repositories\CompanyRepository.cs:line 39
   at BLL.Managers.CompanyManager.GetAllAlowedCompanies(ClaimsPrincipal user) in E:\src\BLL\Managers\CompanyManager.cs:line 44
   at API.Controllers.CompanyController.GetAllCompanies() in E:\src\API\Controllers\CompanyController.cs:line 44
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()

Further technical details

EF Core version: (found in project.json or packages.config)
Operating system: Windows 10 Pro
Visual Studio version: Visual Studio 15 Professional

Other details about my project setup:
ASP.NET Core
.NET Core 1.0.1

@ErazerBrecht
Copy link
Author

Because of this http://stackoverflow.com/a/40328769/2961887. I learnt to use LinqKit.

This did resolve my issue. You can close this issue.

@smitpatel
Copy link
Contributor

dupe of #6132
This is an issue with Re-Linq query parser

@zhenlei520
Copy link

Because of this http://stackoverflow.com/a/40328769/2961887, i solved my problem,Thank you very much

image

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants