dotnet / dotnet/efcore

Query: Support First over subquery which returns enumerable result

Open
#27,867 1 comment 0 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Hi I have a problem with a query. Executing a query which seems OK produces ArgumentException with a message which doesn't tell anything about the error. I spent approximately 1 hour localising the error and it is in the query itself.

**The problem is that I need to use the broken query because I explicitly need only WORKFLOW from the _first_ SADA_WORKFLOW to force 1 to 1 relation constraint (note: I am using DB-First), not from all SADA_WORKFLOW enities!**
**The second query which works, does not force the 1 to 1 relation.**

Is there, please, any way to achieve desired results from the query?

Data model:
![image](https://user-images.githubusercontent.com/64279914/164803485-d56f5f74-68d2-40e2-848a-e0217787ac90.png)

Dictionary:
_Workflow_ = workflow configuration for a system module for a project (finite automaton)
_Sada workflow_ = a reusable/shared collection of workflows
_Stavebni Objekt_ = building
_Projekt_ = project
_Konfigurace projektu_ = project configuration
_Typ x_ = a type/kind of x

Please see the following comparison:

The difference is:

![image](https://user-images.githubusercontent.com/64279914/164792431-7998697a-6bbc-4137-a11e-2b48555b59fb.png)

-----------------------------------------

Sample project and database here:
The project assumes that the database is loaded as `EFCore6QueryArgumentException` on `.\SQLEXPRESS` (local named SQL Server Express instance).

[issue.zip](https://github.com/dotnet/efcore/files/8543997/issue.zip)

-----------------------------------------

Running the "broken" query produces this exception:

```
System.ArgumentException: ' Argument type
'System.Collections.Generic.ICollection`1[Tempus.Vystavba.Data.Models.WORKFLOW]'
does not match the corresponding member type
'Tempus.Vystavba.Data.Models.WORKFLOW' Arg_ParamName_Name'
```

-----------------------------------------

Repro video:

https://user-images.githubusercontent.com/64279914/164792695-83111d50-3eb2-4dec-a11f-cde1047c71b2.mp4

-----------------------------------------

Exception details:
```
System.ArgumentException
HResult=0x80070057
Message= Argument type 'System.Collections.Generic.ICollection`1[Tempus.Vystavba.Data.Models.WORKFLOW]' does not match the corresponding member type 'Tempus.Vystavba.Data.Models.WORKFLOW' Arg_ParamName_Name
Source=System.Linq.Expressions
StackTrace:
at System.Linq.Expressions.Expression.ValidateNewArgs(ConstructorInfo constructor, ReadOnlyCollection`1& arguments, ReadOnlyCollection`1& members)
at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments, IEnumerable`1 members)
at System.Linq.Expressions.NewExpression.Update(IEnumerable`1 arguments)
at System.Linq.Expressions.ExpressionVisitor.VisitNew(NewExpression node)
at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessSelectMany(NavigationExpansionExpression source, LambdaExpression collectionSelector, LambdaExpression resultSelector)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
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_0`1.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 Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.Single[TSource](IQueryable`1 source)
at Tempus.Vystavba.Data.DAOs.Test.StavDAO.GetInicialniStavKontrolaStavbyBroken(Int32 stavebniObjektID) in C:\Users\janse\Desktop\issue\Tempus.Vystavba.Backend\Tempus.Vystavba.Data\StavDAO.cs:line 24
at Program.$(String[] args) in C:\Users\janse\Desktop\issue\Tempus.Vystavba.Backend\Tempus.Vystavba.Services.Test\Program.cs:line 13

```

---------------------------------------------

System info:
Microsoft Visual Studio Community 2022 (64-bit) - Preview
Version 17.2.0 Preview 4.0
Windows 10 Home 21H1
Host (useful for support):
Version: 6.0.4
Commit: be98e88c76
Microsoft.EntityFrameworkCore.SqlServer 6.0.4
EFCorePowerTools 2.5.918

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.