dotnet / dotnet/efcore

Enumerable depth

Open
#30,042 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

Hello,

having this model:

```
public class Continent
{
public Guid ContinentID { get; set; }
public string ContinentName { get; set; }
public virtual ICollection Nations { get; set; }
}

public class Nation
{
public Guid NationID { get; set; }
public string NationName { get; set; }
public virtual Continent Continent { get; set; }
public virtual ICollection Cities { get; set; }
}

public class City
{
public Guid CityID { get; set; }
public virtual Nation Nation { get; set; }
public string CityName { get; set; }
public virtual ICollection Districts { get; set; }
}

public class District
{
public Guid DistrictID { get; set; }
public string DistrictName { get; set; }
public virtual City City { get; set; }
public virtual ICollection Streets { get; set; }
}

public class Street
{
public Guid StreetID { get; set; }
public string StreetName { get; set; }
public virtual District District { get; set; }
}

```
this query

`ctx.Set().Select(c => c.Nations.Select(n=> n.Cities.Select(c => c.Districts))).ToArray();
`
works, but if I add another step

`ctx.Set().Select(c => c.Nations.Select(n=> n.Cities.Select(c => c.Districts.Select(d => d.Streets)))).ToArray();
`
fails with :

```
System.ArgumentException
HResult=0x80070057
Messaggio=GenericArguments[2], 'System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.Collections.Generic.ICollection`1[ConsoleApp1.Street]]]', on 'Void PopulateCollection[TCollection,TElement,TRelatedEntity](Int32, Microsoft.EntityFrameworkCore.Query.QueryContext, System.Data.Common.DbDataReader, Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryResultCoordinator, System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Func`5[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,Microsoft.EntityFrameworkCore.Query.Internal.ResultContext,Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryResultCoordinator,TRelatedEntity])' violates the constraint of type 'TRelatedEntity'.
Origine=System.Private.CoreLib
Analisi dello stack:
in System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
in System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
in Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(Expression extensionExpression)
in System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
in Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression, RelationalCommandCache& relationalCommandCache, LambdaExpression& relatedDataLoaders, Int32& collectionId)
in Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
in Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
in System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
in System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
in Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
in Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.b__0()
in Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
in Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
in Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
in System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
in System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
in System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
in ConsoleApp1.Program.Main(String[] args) in C:\EFError\ConsoleApp1\ConsoleApp1\Program.cs: riga 17

VerificationException: Method Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+ShaperProcessingExpressionVisitor.PopulateCollection: type argument 'System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.Collections.Generic.ICollection`1[ConsoleApp1.Street]]]' violates the constraint of type parameter 'TRelatedEntity'.

```
Provider : Microsoft.EntityFrameworkCore.SqlServer 6.0.13

[ConsoleApp1.zip](https://github.com/dotnet/efcore/files/10402355/ConsoleApp1.zip)

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.