gpt4 book ai didi

c# - 无法计算表达式 - Linq 查询只返回 null

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:41 24 4
gpt4 key购买 nike

我真的不确定这里发生了什么。我目前正在我的 Web Api 中处理一些操作,其中一个涉及使用标识符检查我的数据库中是否存在联系人,然后检索联系人是否存在。

所以我有我的表达方式:

var contact = context.Contacts.Where(x => x.UniqueId == Identifier).FirstOrDefault();

if (contact == null) { return "Identifier not recognised. Check correct type and Identifier"; }

//Goes on to perform tasks with contact information.

然后它继续......但这是它表现得很奇怪的地方。

原始的 linq 查询什么都不做。它不会检索联系人供以后使用,甚至不会注册为 null 来触发返回消息。

在我的调试控制台中,联系人字段只是变灰(可见,具有空值,但无法单击),当我点击值列中的刷新按钮时,它说

Could not evaluate expression

这可能是什么原因?我访问同一个数据库的其他 API 服务工作得非常好,只是这一行。

可能相关的事实

  • 我正在使用的两个命名空间中名为“contact”的两个实体之间存在类型冲突。我不认为这会很重要。类型转换没有解决它。只是想我会提到它。

最佳答案

我做了一些上面评论中建议的更改,还明确定义了联系人,现在它可以很好地从数据库中加载联系人。我真的很困惑为什么它基本上只是重构,但以下是有效的:

namespace.Models.Contact contact = context.Contacts.FirstOrDefault(x => x.UniqueId == Identifier);

//If Identifier isn't recognised return.
if (contact == default(namespace.Models.Contact))
{
return "Identifier not recognised. Check correct type and Identifier";
}

这个工作代码在功能上与我问题中发布的代码没有什么不同这一事实让我感到困惑。我想这一定是我的 VS 正在做的事情。

Alex Pavlov 在评论中指向 https://weblog.west-wind.com/posts/2013/Nov/21/Visual-Studio-2013-Could-not-evaluate-Expression-Debugger-Abnormality

这段代码非常好,如果它只是表现得很奇怪,我可能会通过重新启动 VS 来修复它。

关于c# - 无法计算表达式 - Linq 查询只返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com