C# LINQ to SQL 和空字符串,我如何使用包含?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/977159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:42:12  来源:igfitidea点击:

LINQ to SQL and Null strings, how do I use Contains?

c#.netlinq-to-sql

提问by Oakcool

Here is the query

这是查询

from a in this._addresses
where a.Street.Contains(street) || a.StreetAdditional.Contains(streetAdditional)
select a).ToList<Address>()

if both properties in the where clause have values this works fine, but if for example, a.StreetAdditional is null (Most of the times), I will get a null reference exception.

如果 where 子句中的两个属性都有值,这可以正常工作,但例如,如果 a.StreetAdditional 为空(大多数情况下),我将收到空引用异常。

Is there a work around this?

有解决这个问题的方法吗?

Thanks,

谢谢,

采纳答案by driis

The most obvious one:

最明显的一个:

from a in this._addresses
where (a.Street != null && a.Street.Contains(street)) || (a.StreetAdditional != null && a.StreetAdditional.Contains(streetAdditional))
select a).ToList<Address>()

Alternatively you could write an extension method for Contains that accepts a null argument without error. Some might say that it is not so pretty to have such a method, because it looks like a normal method call, but is allowed for null values (thereby setting aside normal null-checking practices).

或者,您可以为 Contains 编写一个扩展方法,该方法接受空参数而不会出错。有些人可能会说,拥有这样的方法并不是那么漂亮,因为它看起来像一个普通的方法调用,但允许使用空值(从而搁置正常的空检查实践)。

回答by Yaakov Ellis

Check to make sure that the properties are not null

检查以确保属性不为空

from a in this._addresses
where (a.Street != null && a.Street.Contains(street)) || 
(a.StreetAdditional != null && a.StreetAdditional.Contains(streetAdditional))
select a).ToList<Address>()

If the null check is false, then the second clause after the && will not evaluate.

如果空检查为假,则 && 之后的第二个子句将不求值。

回答by Dimi Takis

from a in this._addresses
where a.Street.Contains(street) || (a.StreetAdditional != null && a.StreetAdditional.Contains(streetAdditional)
select a).ToList<Address>()

回答by Dario

You must check first if StreetAdditionalis null.

您必须先检查是否StreetAdditionalnull

Try

尝试

where a.Street.Contains(street) || ((a != null) && a.StreetAdditional.Contains(streetAdditional))

This works because &&is a shortcut-operatorand if a != nullyields false, the second expression with the null-value won't be evaluated since the result will be falseanyway.

这是有效的,因为它&&是一个快捷操作符,如果a != null产生 false,null则不会评估带有-value的第二个表达式,因为false无论如何结果都是如此。

回答by Vasu Balakrishnan

I would create an extension method to return an empty sequence if null and then call contains method.

如果为 null,我将创建一个扩展方法以返回一个空序列,然后调用 contains 方法。

public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> pSeq)
{
      return pSeq ?? Enumerable.Empty<T>();
}

from a in this._addresses
where a.Street.Contains(street) || 
      a.StreetAdditional.EmptyIfNull().Contains(streetAdditional)
select a).ToList<Address>()

回答by Yuliy

I'd use the null-coalescing operator...

我会使用空合并运算符...

(from a in this._addresses
where (a.Street ?? "").Contains(street) || (a.StreetAdditional ?? "").Contains(streetAdditional)
select a).ToList<Address>()

回答by Amy B

I don't think SqlServer gave you a null exception. If it did, then this code is clearly not running though LinqToSql (as you've tagged the question).

我不认为 SqlServer 给了你一个空异常。如果是这样,那么此代码显然没有通过 LinqToSql 运行(因为您已经标记了问题)。

string.Contains would be translated to sql's like, which has no trouble at all with null values.

string.Contains 将被转换为 sql's like,它对于空值完全没有问题。

回答by Brian

You might want to check to make sure the variables street and streetAdditional are not null. I just ran across the same problem and setting them to an empty string seemed to solve my problem.

您可能需要检查以确保变量 street 和 streetAdditional 不为空。我刚刚遇到了同样的问题,并将它们设置为空字符串似乎解决了我的问题。

street = street ?? "";
streetAdditional = streetAdditional ?? "";
from a in this._addresses
where a.Street.Contains(street) || a.StreetAdditional.Contains(streetAdditional)
select a).ToList<Address>()

回答by user5636696

One thing to note is that the null should be evaluated first.

需要注意的一件事是应该首先评估空值。

where (**a.Street != null** && a.Street.Contains(street)) || (a.StreetAdditional != null && a.StreetAdditional.Contains(streetAdditional))
select a).ToList<Address>

()

()