- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我将我的项目更新到 dotnet core 3.0RC1(可能也在预览版 9 中)之后,我的代码曾经可以工作
var value = context.Products.Where(t => t.CategoryId == catId).Select(t => t.Version).DefaultIfEmpty().Max();
开始 throw System.InvalidOperationException:序列不包含任何元素
。有问题的表是空的。
如果我添加 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 core产生的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
最佳答案
所以我打开了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()。
关于c# - DefaultIfEmpty().Max() 仍然抛出 'Sequence contains no elements.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58039509/
快速且可能简单的 Lambda 问题: 我有一家有评论的餐厅。我想查询具有以下内容的那个: 最大(平均评分) 和 Max(ReviewCount) 和 Max(NewestReviewDate) 和
在尝试使用 C++17 折叠表达式时,我尝试实现 max sizeof ,其中结果是类型 sizeof 的最大值。我有一个使用变量和 lambda 的丑陋折叠版本,但我想不出一种使用折叠表达式和 st
我目前正在使用 C 并遇到了一些我觉得有趣的东西,但似乎在这里找不到任何类似的东西。 我正在为数组(大小 1000000)静态分配内存。我知道这相当大并且有可能引起问题。但是,使用 10^6 不会出现
我有一个具有 max-height 的 div 和其中的图像,应该使用 max-width:100% 和 max-height:100%。在 Chromium 中,这是可行的,但 Firefox 仅使
我有一个最大高度的 div 和里面的一个图像,它应该使用最大宽度:100% 和最大高度:100%。在 Chromium 中,这是可行的,但 Firefox 仅使用最大宽度而忽略最大高度。 div#ov
在一本在线 awk 手册中我找到了例子awk '{ if (NF > max) max = NF } END { print max }' 该程序打印任何输入行上的最大字段数。但我不明白 awk 如何
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在制作一个非循环图数据库。 表 Material (id_item,id_collection,...)主键(id_item,id_collection) (item可以是collection本身
我有以下两个表。 1.电影详情(电影ID、电影名称、评分、票数、年份) 2.电影类型(Movie-ID,Genre) 我正在使用以下查询来执行连接并获得每个评分最高的电影流派。 select Movi
我有一个查询,我想返回 idevent 中给定传感器 ID (sensorID) 范围内的最高 ID 值,但是查询没有返回最高值。 我运行查询时减去 max() 语句的结果: mysql> SELEC
SUM(MAX() + MAX()) 有正确的方法吗? 这是我一直在努力做的事情 SELECT SUM(MAX(account.BALANCE1) + MAX(account.BALANCE2))
这个问题类似于CSS media queries: max-width OR max-height , 但由于我的代表不够高,我无法在回复中添加评论(问题),我想在原始问题中添加。 与其他主题中的发帖
Jon Skeet今天报告(source): Math.Max(1f, float.NaN) == NaN new[] { 1f, float.NaN }.Max() == 1f 为什么? 编辑:双倍
这个问题已经有答案了: Java 8 stream's .min() and .max(): why does this compile? (5 个回答) 已关闭 7 年前。 我正在学习1z0-809
我在处理一些数据库记录时遇到了一些挑战。 我需要为特定列获取具有 MAX 值的行,并且这些记录必须介于两个时间戳值之间。 这是SQL查询 SELECT id, MAX(amount), created
我想在媒体查询中使用 AND 条件。我使用了下面的代码,但是没有用 @media screen and (max-width: 995px AND max-height: 700px) { } 最佳答
在编写 CSS 媒体查询时,有什么方法可以用“或”逻辑指定多个条件吗? 我正在尝试做这样的事情: /* This doesn't work */ @media screen and (max-widt
我对仅使用 max(list array) 和 np.max(list array) 之间的区别有疑问。 这里唯一的区别是 Python 返回代码所需的时间吗? 最佳答案 它们在边缘情况下可能不同,例
例如: a = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
这个问题在这里已经有了答案: Java 8 stream's .min() and .max(): why does this compile? (5 个答案) 关闭 6 年前。 我正在学习 1z0
我是一名优秀的程序员,十分优秀!