C# 自定义LINQ扩展方法引发VerificationException:类型参数IEnumerable“1[entity]”违反类型参数“TCollection”的约束

C# 自定义LINQ扩展方法引发VerificationException:类型参数IEnumerable“1[entity]”违反类型参数“TCollection”的约束,c#,entity-framework-core,asp.net-core-mvc,C#,Entity Framework Core,Asp.net Core Mvc,我有一些实体看起来像这样: public class MyEvent { // ... public IEnumerable<MyEncounter> Encounters { get; set; } // Navigation property // ... } public class MyEncounter { public Guid MyEventID { get; set; } public MyEvent MyEvent { g

我有一些实体看起来像这样:

public class MyEvent 
{
    // ...
    public IEnumerable<MyEncounter> Encounters { get; set; } // Navigation property
    // ...
}

public class MyEncounter
{
    public Guid MyEventID { get; set; }
    public MyEvent MyEvent { get; set; }  // Navigation property

    public Guid? MySpecialProperty { get; set; }

    public int Status { get; set; }
    //...
}
内部异常仅稍微详细一些:

ArgumentException: GenericArguments[0], 'System.Collections.Generic.IEnumerable`1[<fully qualified name of MyEncounter>]', on 'Void PopulateCollection[TCollection,TElement,TRelatedEntity](Int32, Microsoft.EntityFrameworkCore.Query.QueryContext, System.Data.Common.DbDataReader, Microsoft.EntityFrameworkCore.Query.Internal.ResultCoordinator, 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.Func`5[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,Microsoft.EntityFrameworkCore.Query.Internal.ResultContext,Microsoft.EntityFrameworkCore.Query.Internal.ResultCoordinator,TRelatedEntity])' violates the constraint of type 'TCollection'.
工作原理是在查询中执行所有逻辑,而不使用任何扩展方法:

// Query
var qry = MyEventsContext.
    .Where(<some logic>)
    .Select(x => new MyViewModel
    {
        SpecialPropValue = x.Encounters.Where(e => e.Status == 1)
                                       .Select(x => x.MySpecialProperty)
                                       .FirstOrDefault()   
    });
调用x.Conferences上的ToList对我在3.1 EF Core中有效,但可计算和ToArray不:

.Select(x => new MyViewModel
{
    SpecialPropValue = x.Encounters.ToList().GetMySpecialProperty()
});
此类行为的原因是类型问题-方法尝试处理填充集合,并尝试构建表达式树以调用方法,该方法具有集合类型为ICollection的泛型约束,但确定IEnumerable并将其传递给调用,失败原因是IEnumerable不是ICollection,反之亦然实际上。

调用x.Conferences上的ToList在3.1 EF Core中对我有效,尽管可计算和ToArray不:

.Select(x => new MyViewModel
{
    SpecialPropValue = x.Encounters.ToList().GetMySpecialProperty()
});

此类行为的原因是类型问题-方法尝试处理填充集合,并尝试构建表达式树以调用方法,该方法具有集合类型为ICollection的泛型约束,但确定IEnumerable并将其传递给调用,但调用失败,因为IEnumerable不是ICollection,事实上反之亦然。

代码不可编译。@TanveerBadar它不是真正要编译的。我已经包括了一堆不同文件中相关内容的片段。我应该更清楚一些。您的MyEvent导航道具缺少名称,因此有点难以理解。不管它是否编译,精神上很难编译。另外,看看使用ToList是否能解决这个问题。我自己也注意到异步方法周围有一些奇怪的行为。@TanveerBadar啊,我明白了!我已经纠正了那个错误。我还尝试在执行查询时使用.ToList,但它没有改变任何东西。您的代码没有编译。@TanveerBadar它不是真正要编译的。我已经包括了一堆不同文件中相关内容的片段。我应该更清楚一些。您的MyEvent导航道具缺少名称,因此有点难以理解。不管它是否编译,精神上很难编译。另外,看看使用ToList是否能解决这个问题。我自己也注意到异步方法周围有一些奇怪的行为。@TanveerBadar啊,我明白了!我已经纠正了那个错误。我还尝试在执行查询时使用.ToList,但它没有改变任何东西。
ArgumentException: GenericArguments[0], 'System.Collections.Generic.IEnumerable`1[<fully qualified name of MyEncounter>]', on 'Void PopulateCollection[TCollection,TElement,TRelatedEntity](Int32, Microsoft.EntityFrameworkCore.Query.QueryContext, System.Data.Common.DbDataReader, Microsoft.EntityFrameworkCore.Query.Internal.ResultCoordinator, 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.Func`5[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,Microsoft.EntityFrameworkCore.Query.Internal.ResultContext,Microsoft.EntityFrameworkCore.Query.Internal.ResultCoordinator,TRelatedEntity])' violates the constraint of type 'TCollection'.
// Extension method
public static IEnumerable<MyEncounter> GetMySpecialEncounters(this IEnumerable<MyEncounter> encounters)
{
    return encounters.Where(e => e.Status == 1)
}

// Query
var qry = MyEventsContext.
    .Where(<some logic>)
    .Select(x => new MyViewModel
    {
        SpecialPropValue = x.Encounters.GetMySpecialEncounters()
                                       .Select(x => x.MySpecialProperty)
                                       .FirstOrDefault()   
    });
When called from 'VisitLambda', rewriting a node of type 'System.Linq.Expressions.ParameterExpression' must return a non-null value of the same type
// Query
var qry = MyEventsContext.
    .Where(<some logic>)
    .Select(x => new MyViewModel
    {
        SpecialPropValue = x.Encounters.Where(e => e.Status == 1)
                                       .Select(x => x.MySpecialProperty)
                                       .FirstOrDefault()   
    });
.Select(x => new MyViewModel
{
    SpecialPropValue = x.Encounters.ToList().GetMySpecialProperty()
});