Repository navigation
Description
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
smitpatel commentedon Sep 19, 2019
Duplicate of #16314. We cannot create Queryables in result. Add
ToListafterPurchases.Where(…)N+1 because that is what expression tree gives us.
ericjohannsen commentedon Sep 19, 2019
Is that the same reason I must add .ToList() in this query?
smitpatel commentedon Sep 19, 2019
Yes. Basically the type of
Purchasesin your anonymous type cannot be IQueryable. It can be any Enumerable type.smitpatel commentedon Sep 20, 2019
This would be perfect work-around #17944 (comment)