LINQ 列表在 where 子句中给出 null 异常

发布于 2024-11-15 20:44:35 字数 562 浏览 2 评论 0原文

var q = from p in query
where 
   ((criterias.birthday == p.BirthDay|| criterias.birthday == null))                                
&& ((criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate))                                
&& ((criterias.gender == p.Gender) || (criterias.gender == null))
&& ((criterias.nationalities.Contains(p.Nationality)) || (criterias.nationalities == null))

criterias 是一个我存储搜索条件的类。 Nationalities 是一个字符串列表。当我的字符串中没有项目时,就会出现问题。查询抛出空引用异常。该查询不接受国籍中的空值。我该如何解决这个问题?

var q = from p in query
where 
   ((criterias.birthday == p.BirthDay|| criterias.birthday == null))                                
&& ((criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate))                                
&& ((criterias.gender == p.Gender) || (criterias.gender == null))
&& ((criterias.nationalities.Contains(p.Nationality)) || (criterias.nationalities == null))

criterias isa class where i store my search criterias. nationalities is a string list. the problem occurs when i have no items in string. the query throws null reference exception. the query doesnt accept null value in nationalities. how can i fix this?

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

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

发布评论

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

评论(5

海螺姑娘 2024-11-22 20:44:35

颠倒顺序,以便空检查出现在查询之前:当您使用 || 时,仅当第一部分计算结果为 false 时才计算表达式的第二部分:

&& ((criterias.nationalities == null) || 
             (criterias.nationalities.Contains(p.Nationality)))

Reverse the order so that the null check comes before the query: as you're using ||, the second part of the expression is only evaluated when the first part evaluates to false:

&& ((criterias.nationalities == null) || 
             (criterias.nationalities.Contains(p.Nationality)))
情域 2024-11-22 20:44:35

看看这 2 个:

   ((criterias.birthday == p.BirthDay|| criterias.birthday == null))
&& ((criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate))  

我不认为 weddingDate 会给你带来问题,但生日使用了错误的顺序。
在这种情况下,您需要 || 的“短路评估”属性,将其更改为:

   (criterias.birthday == null || criterias.birthday == p.BirthDay)
&& (criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate)

Look at these 2:

   ((criterias.birthday == p.BirthDay|| criterias.birthday == null))
&& ((criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate))  

I don't think marriageDate will give you problems but birthday uses the wrong order.
In this case you need the 'short-circuit evaluation' property of ||, change it to:

   (criterias.birthday == null || criterias.birthday == p.BirthDay)
&& (criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate)
童话 2024-11-22 20:44:35

尝试交换国籍检查的顺序。在尝试评估 Contains 之前,它应该在空检查上短路。

((criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))) 

Try swapping the order of the nationalties checks. It should short-circuit on the null check before it tries to evaluate the Contains.

((criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))) 
北城挽邺 2024-11-22 20:44:35

翻转此语句:

(criterias.nationalities.Contains(p.Nationality)) || (criterias.nationalities == null)

使其显示为“

(criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))

如果第一个操作数的计算结果为 true,则将跳过第二个操作数”。

Turn this statement around:

(criterias.nationalities.Contains(p.Nationality)) || (criterias.nationalities == null)

so that it reads

(criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))

If the first operand evaluates to true, the second one will be skipped.

忘羡 2024-11-22 20:44:35

尝试首先检查是否为空,然后(如果不为空)检查是否包含:

var q = from p in query 
where     
((criterias.birthday == p.BirthDay|| criterias.birthday == null))                                 
&& ((criterias.marriageDate == null ||  criterias.marriageDate == p.MarriageDate))
&& ((criterias.gender == p.Gender) || (criterias.gender == null)) 
&& ((criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))

Try first check for null then (if it is not null) check for contains:

var q = from p in query 
where     
((criterias.birthday == p.BirthDay|| criterias.birthday == null))                                 
&& ((criterias.marriageDate == null ||  criterias.marriageDate == p.MarriageDate))
&& ((criterias.gender == p.Gender) || (criterias.gender == null)) 
&& ((criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文