问题
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
, returntrue
.Any(a => a.NMC_Number == null || a.NMC_Number == model.NMC_Number);
- If
is not null
, returntrue
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