在Linq中使用ANY条件处理WHERE中的空值 [英] Handling null values inside a WHERE with ANY Condition in linq

查看:47
本文介绍了在Linq中使用ANY条件处理WHERE中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是db对象可以具有空描述,ofc linq将使用ANY引发Null Exception.我正在用它来搜索描述和标题.

这是代码

  string [] searchQry = searchString.Split('');searchQry = searchQry.Select(sq => sq.ToLower()).ToArray();searchResult = x.reportsInfo哪里(r =>searchQry.Any(y => r.report.Description.ToLower().Contains(y))||searchQry.Any(y => r.report.Title.ToLower().Contains(y))).ToList(); 

尝试使用三元条件运算符将其封装.

解决方案

如何:

 y =>r.report.Description?.ToLower().包含(y)??错误的 

这使用空传播运算符(?)来避免在 Description null 的情况下出现 NullReferenceException 如果它是 null ,那么我们使用null合并运算符( ?? )提供默认值(在这种情况下为 false ).

您可以将两个 Any 调用合并为一个:

  searchQry.Any(r => r.report.Description?.ToLower().Contains(y)?? false||r.report.Title.ToLower().包含(y)) 

如果要先在每个 Description 上应用标准,然后再在 Title 上应用该标准,则需要坚持使用两个单独的任何呼叫:

  .Where(r => searchQry.Any(y => r.report.Description?.ToLower().Contains(y)?? false)||searchQry.Any(y => r.report.Title.ToLower().Contains(y))).ToList(); 

另一种方法是在 Any 之前使用 Where 子句以过滤出 Description 为null的记录:

  searchResult = x.reportsInfo.where(r => searchQry.Where(r => r.report.Description!= null).Any(y => r.report.Description.ToLower().Contains(y))|| searchQry.Any(y => r.report.Title.ToLower().Contains(y))).ToList(); 

My problem is the db object can have a null description and ofc linq will throw Null Exception with ANY. I am using this to search both description and title .

here is the code

string[] searchQry = searchString.Split(' ');            
searchQry = searchQry.Select(sq=> sq.ToLower()).ToArray();

searchResult = x.reportsInfo
    .Where(r => 
        searchQry.Any(y => r.report.Description.ToLower().Contains(y)) ||
        searchQry.Any(y => r.report.Title.ToLower().Contains(y)))
    .ToList();

tried encapsulating it with ternary conditional operator.

解决方案

how about:

 y => r.report.Description?.ToLower().Contains(y) ?? false

This uses the null propagation operator (?) to avoid NullReferenceException in the case of Description being null and if it's null then we use the null coalescing operator (??) to provide a default value (false in this case).

btw you can merge both the Any calls into one:

searchQry.Any(r => r.report.Description?.ToLower().Contains(y) ?? false 
                 || r.report.Title.ToLower().Contains(y))

if you want to apply the criteria to each and every Description first before applying it on Title then you'll need to stick with your approach of two individual Any calls:

.Where(r => searchQry.Any(y => r.report.Description?.ToLower().Contains(y) ?? false)
     ||  searchQry.Any(y => r.report.Title.ToLower().Contains(y))).ToList();

Another approach being to use a Where clause before Any to filter out the records where the Description is null:

searchResult = x.reportsInfo
                 .Where(r => searchQry.Where(r => r.report.Description != null)
                                      .Any(y => r.report.Description.ToLower().Contains(y))
                     ||searchQry.Any(y => r.report.Title.ToLower().Contains(y)))                                        
                .ToList();

这篇关于在Linq中使用ANY条件处理WHERE中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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