DefaultIfEmpty()。Max()仍然抛出“序列不包含任何元素”。

时间:2019-09-21 10:45:41

标签: c# .net-core ef-core-3.0

在将项目更新为dotnet core 3.0RC1(也可能在Preview9中)后,我的代码可以正常工作

var value = context.Products.Where(t => t.CategoryId == catId).Select(t => t.Version).DefaultIfEmpty().Max();

开始投掷 System.InvalidOperationException: Sequence contains no elements。该表为空。

如果我添加ToList(),使其看起来像DeafultIfEmpty().ToList().Max(),它将重新开始工作。找不到有关重大更改的任何信息。当我运行

var expectedZero = new List<int>().DefaultIfEmpty().Max();

它工作正常。这让我觉得EF Core可能出了点问题。然后,我在xUnit中使用完全相同的设置创建了测试,但是测试通过了(表也为空,使用InMemoryDatabase而不是实时SQL Server实例)。

我真的很困惑。 相关堆栈跟踪:

System.InvalidOperationException: Sequence contains no elements.
   at int lambda_method(Closure, QueryContext, DbDataReader, ResultContext, int[], ResultCoordinator)
   at bool Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+QueryingEnumerable<T>+Enumerator.MoveNext()
   at TSource System.Linq.Enumerable.Single<TSource>(IEnumerable<TSource> source)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
   at TSource System.Linq.Queryable.Max<TSource>(IQueryable<TSource> source)
   at ... (my method that run the code)

修改

产品类别:

   [Table("tmpExtProduct", Schema = "ext")]
    public partial class Product
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int Version { get; set; }

        [Column(TypeName = "datetime")]
        public DateTime ImportDate { get; set; }

        public int CategoryId { get; set; }
        public string Description { get; set; }

        [ForeignKey(nameof(Ext.Category))]
        public int CategoryId { get; set; }        

        [InverseProperty(nameof(Ext.Category.Products))]
        public virtual Category Category { get; set; }
    }

第二次修改 ef核心产生的SQL

exec sp_executesql N'SELECT MAX([t0].[Version])
FROM (
    SELECT NULL AS [empty]
) AS [empty]
LEFT JOIN (
    SELECT [p].[Version], [p].[CategoryId], [p].[ImportDate], [p].[Description]
    FROM [ext].[tmpExtProduct] AS [p]
    WHERE (([p].[CategoryId] = @__categoryId_0) AND @__categoryId_0 IS NOT NULL)
) AS [t0] ON 1 = 1',N'@__categoryId_0 int',@__categoryId_0=5

1 个答案:

答案 0 :(得分:1)

所以我打开了issue in EF Core repo并得到了答案。显然,这是当前的行为,以后可能会更改。

建议使用以下方法

var valueFail = context.Products.Where(t => t.CategoryId == catId)
                .GroupBy(e => 1)
                .Select(t => t.Max(e => e.Version))
                .ToList()[0];

这比我的解决方法DeafultIfEmpty().ToList().Max()更好,因为它将完成服务器端的所有工作,而我的解决方案将在客户端上计算Max()。