使用 LINQ 管理 null

发布于 2024-10-22 00:38:46 字数 281 浏览 3 评论 0原文

使用此代码

 var res = (from p in list where 
           p.FirstName.ToUpper() == firstName.ToUpper() || 
           p.LastName.ToUpper() == lastName.ToUpper() select p).ToList<Client>();

p.FirstName 或 firstName 可以为 NULL 我该如何管理?

谢谢,

With this code

 var res = (from p in list where 
           p.FirstName.ToUpper() == firstName.ToUpper() || 
           p.LastName.ToUpper() == lastName.ToUpper() select p).ToList<Client>();

The p.FirstName, or the firstName can be NULL how can I manage this ?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷了相思 2024-10-29 00:38:46

像这样:

where String.Equals(p.LastName, lastName, StringComparison.OrdinalIgnoreCase)

Like this:

where String.Equals(p.LastName, lastName, StringComparison.OrdinalIgnoreCase)
旧情勿念 2024-10-29 00:38:46

使用 String.Equals() 指定不区分大小写。

Use String.Equals() specifying case insensitivity.

甲如呢乙后呢 2024-10-29 00:38:46

我通常在处理不区分大小写的字符串时使用 string.Compare,例如:

bool areEquivalent = string.Compare(s1, s2, true) == 0;

I usually use string.Compare when dealing with non-case-sensitive strings, like:

bool areEquivalent = string.Compare(s1, s2, true) == 0;
夏花。依旧 2024-10-29 00:38:46
var res = (from p in list where 
       p.FirstName != null && firstName != null && p.FirstName.ToUpper() == firstName.ToUpper() || 
       p.LastName.ToUpper() == lastName.ToUpper() select p).ToList<Client>();
var res = (from p in list where 
       p.FirstName != null && firstName != null && p.FirstName.ToUpper() == firstName.ToUpper() || 
       p.LastName.ToUpper() == lastName.ToUpper() select p).ToList<Client>();
森罗 2024-10-29 00:38:46

像这样的东西吗?

var res = (from p in list where 
    (p.FirstName ?? "").ToUpper() == (firstName ?? "").ToUpper() || 
    (p.LastName ?? "").ToUpper() == (lastName ?? "").ToUpper() select p)
    .ToList<Client>();

这实际上取决于当其中一个为 null 时您想要做什么。出于比较目的,这应该将 null 视为等同于空字符串。

Something like this?

var res = (from p in list where 
    (p.FirstName ?? "").ToUpper() == (firstName ?? "").ToUpper() || 
    (p.LastName ?? "").ToUpper() == (lastName ?? "").ToUpper() select p)
    .ToList<Client>();

It really depends on what you want to do when either is null. This should treat null as equivalent to an empty string for comparison purposes.

三生池水覆流年 2024-10-29 00:38:46

您可以使用 string.Compare 来实现此目的。

string x = null;
string y = null;

//both NUnit tests pass
Assert.IsTrue(string.Equals(x, y));
Assert.AreEqual(0, string.Compare(x, y, true));

...

var ignoreCase = true;
var res = (from p in list 
           where string.Compare(p.FirstName, firstName, ignoreCase) == 0 || 
                 string.Compare(p.LastName, lastName, ignoreCase) == 0 
           select p).ToList<Client>();

You can use string.Compare for this.

string x = null;
string y = null;

//both NUnit tests pass
Assert.IsTrue(string.Equals(x, y));
Assert.AreEqual(0, string.Compare(x, y, true));

...

var ignoreCase = true;
var res = (from p in list 
           where string.Compare(p.FirstName, firstName, ignoreCase) == 0 || 
                 string.Compare(p.LastName, lastName, ignoreCase) == 0 
           select p).ToList<Client>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文