Skip to content

Select equivalent of GroupJoin throws or creates 1 + N queries #17944

Closed as not planned

Description

@ericjohannsen

Rewriting the query mentioned here to use selects results in an exception if executed on the server or creates 1 + N queries if forced to execute locally

Steps to reproduce

Create the trivial database described in the link above.

Run this query:

var selectEquivalent =
	from c in Customers
	select new
	{
		CustName = c.Name,
		custPurchases = Purchases.Where (p => c.ID == p.CustomerID)
	};

Throws:

ArgumentException: Argument type 'System.Collections.Generic.List1[Purchase]' does not match the corresponding member type 'System.Linq.IQueryable1[Purchase]' (Parameter 'arguments[1]')

at System.Linq.Expressions.Expression.ValidateNewArgs(ConstructorInfo constructor, ReadOnlyCollection1& arguments, ReadOnlyCollection1& members)
at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable1 arguments, IEnumerable1 members)
at System.Linq.Expressions.NewExpression.Update(IEnumerable1 arguments) at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression) at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector) at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_01.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.System.Collections.IEnumerable.GetEnumerator()

--

Forcing the query client-side:

var selectEquivalent =
	from c in Customers.AsEnumerable()
	select new
	{
		CustName = c.Name,
		custPurchases = Purchases.Where (p => c.ID == p.CustomerID)
	};

Results in one additional query per customer:

SELECT [c].[ID], [c].[Name]
FROM [Customer] AS [c]
GO

-- Region Parameters
-- @__c_ID_0='1' (Nullable = true)
-- EndRegion
SELECT [p].[ID], [p].[CustomerID], [p].[Date], [p].[Description], [p].[Price]
FROM [Purchase] AS [p]
WHERE ((@__c_ID_0 = [p].[CustomerID]) AND (@__c_ID_0 IS NOT NULL AND [p].[CustomerID] IS NOT NULL)) OR (@__c_ID_0 IS NULL AND [p].[CustomerID] IS NULL)
GO

-- Region Parameters
-- @__c_ID_0='2' (Nullable = true)
-- EndRegion
SELECT [p].[ID], [p].[CustomerID], [p].[Date], [p].[Description], [p].[Price]
FROM [Purchase] AS [p]
WHERE ((@__c_ID_0 = [p].[CustomerID]) AND (@__c_ID_0 IS NOT NULL AND [p].[CustomerID] IS NOT NULL)) OR (@__c_ID_0 IS NULL AND [p].[CustomerID] IS NULL)
GO

-- Region Parameters
-- @__c_ID_0='3' (Nullable = true)
-- EndRegion
SELECT [p].[ID], [p].[CustomerID], [p].[Date], [p].[Description], [p].[Price]
FROM [Purchase] AS [p]
WHERE ((@__c_ID_0 = [p].[CustomerID]) AND (@__c_ID_0 IS NOT NULL AND [p].[CustomerID] IS NOT NULL)) OR (@__c_ID_0 IS NULL AND [p].[CustomerID] IS NULL)
GO
-- Etc

Further technical details

EF Core version: 3.0.0-rc1.19456.14
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.2.3

Activity

  1. smitpatel commented on Sep 19, 2019

    @smitpatel
    Contributor

    Duplicate of #16314. We cannot create Queryables in result. Add ToList after Purchases.Where(…)

    N+1 because that is what expression tree gives us.

  2. ericjohannsen commented on Sep 19, 2019

    @ericjohannsen
    Author

    Is that the same reason I must add .ToList() in this query?

    from c in Customers
    select new
    {
    	c.Name,
    	Purchases = (
    		from p in Purchases
    		where p.CustomerID == c.ID && p.Price > 1000
    		select new { p.Description, p.Price }).ToList()
    }
  3. smitpatel commented on Sep 19, 2019

    @smitpatel
    Contributor

    Yes. Basically the type of Purchases in your anonymous type cannot be IQueryable. It can be any Enumerable type.

  4. smitpatel commented on Sep 20, 2019

    @smitpatel
    Contributor

    This would be perfect work-around #17944 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions