简体   繁体   English

如何使用条件空运算符检查空字符串?

[英]How can I use the conditional null operator to check for null string?

I'm trying to execute the LINQ to objects query as follows: 我试图执行LINQ到对象查询,如下所示:

var c1 = allCustomers
    .Where(x => x.CompanyName.Replace("'", "").StartsWith(searchText))
    .ToList();

This works fine as long as CompanyName is not null. 只要CompanyName不为null,就可以正常工作。

So, I thought this seems like the perfect place for the new null conditional operator! 因此,我认为对于新的空条件运算符来说,这似乎是理想的选择! Just change to: 只需更改为:

var c1 = allCustomers
    .Where(x => x.CompanyName?.Replace("'", "").StartsWith(searchText))
    .ToList();

and everything should work! 一切都会正常!

Instead, I get the error: 相反,我得到了错误:

Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

I'm not quite sure how to accomplish what I want here. 我不太确定如何在这里完成我想要的。 How would I use the null conditional in this scenario? 在这种情况下,我将如何使用null条件?

You need a coalesce operator to convert the tri-state to a proper boolean. 您需要合并运算符将三态转换为适当的布尔值。

var c1 = allCustomers
    .Where(x => x.CompanyName?.Replace("'", "").StartsWith(searchText) ?? false)
    .ToList();

I call bool? 我叫bool? a tri-state because it can have three values: true , false and null ; 三态,因为它可以具有三个值: truefalsenull therefore, converting bool? 因此,转换bool? to bool is a narrowing conversion that requires explicit treatment. 转化为bool是一种狭窄的转化,需要进行明确的处理。

you need more question marks! 您需要更多问号! i think you need null coalescing as well, since x.CompanyName?.Replace could now also return null. 我认为您也需要空合并,因为x.CompanyName?.Replace现在也可以返回空。

(x.CompanyName?.Replace("'", "") ?? string.Empty).StartsWith(searchText))

the ?? string.empty ?? string.empty ?? string.empty forces that to be a non-null string, which now supports .startswith ?? string.empty强制将其设置为非null字符串,该字符串现在支持.startswith

Use ?? 使用?? operator like: 运算子,例如:

Where(x => x.CompanyName?.Replace("'", "").StartsWith(searchText) ?? false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Nullable运算符与Null条件运算符一起使用? - How can I use the Nullable Operator with the Null Conditional operator? 如何使用 null 条件运算符安全而简单地检查可为 null 的引用类型的属性 - How can I use the null-conditional operator to safely and briefly check a nullable reference type's property 为什么可以将空条件运算符应用于硬编码字符串? - Why can I apply a null-conditional operator to a hardcoded string? 在Null对象的属性上使用Null条件运算符 - Use Null Conditional Operator on property of Null Object C#6空条件运算符检查.Any()? - C# 6 null conditional operator check for .Any()? 使用条件运算符?检查空会话变量 - Using the conditional operator ? to check for null session variable 如何在空列表中使用空条件运算符? - How to use the null-conditional operator on an empty list? 如何在带有 JsonConvert.DeserializeObject 的动态 JSON 中使用 null 条件运算符 - How to use null conditional operator in a dynamic JSON with JsonConvert.DeserializeObject 使用Null条件运算符检查可能为null的对象上的值 - Using the Null Conditional Operator to check values on objects which might be null null 条件运算符和专用 null 检查之间有什么区别吗? - Is there any difference between the null conditional operator and a dedicated null check?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM