如何检查数据库表中是否存在该值而忽略 LINQ C# 中的 null [英] How to check if the value exist in the database table ignoring null in LINQ C#

查看:85
本文介绍了如何检查数据库表中是否存在该值而忽略 LINQ C# 中的 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当查询使用 LINQ 和 Lambda 表达式检查 C# 中数据库表中存在的值时,我想忽略 null.

I want to ignore null when the query check the value exist in database table in C# using LINQ and Lambda expression.

下面这行也在检查空值.当它发现 null 时,则忽略它.

Below line is checking null as well. When it finds null then ignore it.

var nmcExist = db.AspNetUsers.Any(a => a.NMC_Number== model.NMC_Number);

推荐答案

您可能正在寻找 2 个选项:

You might be looking for 2 options:

  • 如果null,返回true
.Any(a => a.NMC_Number == null || a.NMC_Number == model.NMC_Number);

  • 如果不为空,返回true然后检查第二个条件.
    • If is not null, return true then check the second condition.
    • .Any(a => a.NMC_Number != null && a.NMC_Number == model.NMC_Number);
      

      更新

      正如@Panagiotis Kanavos 的评论:这真的取决于:

      As @Panagiotis Kanavos 's comment: It really depends on:

      • 您实际使用的是哪种 ORM?
      • 哪个版本?

      这仅仅是因为,不同的 ORM,甚至不同的 EF 版本可能会产生不同的 SQL 查询.此外,在 EF v6.2 之前,默认行为是不发出 IS NULL.

      This is simply because, Different ORMs, even different EF versions may produce different SQL queries. Besides, Before EF v6.2, the default behavior was to not emit IS NULL.

      这篇关于如何检查数据库表中是否存在该值而忽略 LINQ C# 中的 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆