How to check if the value exist in the database table ignoring null in LINQ C# [duplicate]

青春壹個敷衍的年華 提交于 2021-01-29 15:47:16

问题


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

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);

回答1:


You might be looking for 2 options:

  • If null, return true
.Any(a => a.NMC_Number == null || a.NMC_Number == model.NMC_Number);
  • If is not null, return true then check the second condition.
.Any(a => a.NMC_Number != null && a.NMC_Number == model.NMC_Number);

Updated

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

  • Which ORM are you actually using?
  • Which version?

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.



来源:https://stackoverflow.com/questions/60430247/how-to-check-if-the-value-exist-in-the-database-table-ignoring-null-in-linq-c-sh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!