【问题标题】:Sequence contains no elements on http post序列在 http post 上不包含任何元素
【发布时间】:2019-04-10 18:13:37
【问题描述】:

在 asp.net 核心的 Httpost 中,我得到 “序列不包含任何元素” 帖子应保存在数据库 Order 和 OrderItems

奇怪的是,在断点调试中我看到序列有元素

var order = new Ordcli
        {
            Id= guid,
            year = model.Year,
            OrderDate = model.Date,
            ...
            OrderItems = model.OrderItems.Select(i => new Rows
            {
                Id = _ctx.Rows.Select(x => x.Id).Max() + 1,
                ProductCode = i.Code,
                Qty = i.Qty
                ...
            }).ToList()
        };

再次,如果我将鼠标放在 OrderItems 上,我会看到该集合

【问题讨论】:

  • _ctx.Rows.Select(x => x.Id).Max() 行导致错误。没有行。
  • 我应该怎么做才能插入一个 ID ?在我的复合键中,我无法添加自动增量
  • 您可以在调用 Max() 之前检查是否有一些行:尝试类似 _ctx.Rows.Any() 的方法? _ctx.Rows.Select(x => x.Id).Ma​​x() + 1 : 1
  • ctx.Rows.Select(x => x.Id).DefaultIfEmpty().Max() + 1,-请试试这个并告诉我
  • 是的,似乎是正确的!非常感谢!再见

标签: c# linq asp.net-core


【解决方案1】:

_ctx.Rows.Select(x => x.Id).Max() 行导致错误。所以将此行替换为以下内容:

ctx.Rows.Select(x => x.Id).DefaultIfEmpty().Max() + 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 2015-02-19
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多