views:

519

answers:

8

Hi all,

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.

Is there a work around this?

Thanks,

+4  A: 

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).

driis
It looks like its workingThanks
Oakcool
Custom extension methods aren't usable in LINQ to SQL.
Pavel Minaev
+1  A: 

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.

Yaakov Ellis
+1  A: 
from a in this._addresses
where a.Street.Contains(street) || (a.StreetAdditional != null && a.StreetAdditional.Contains(streetAdditional)
select a).ToList<Address>()
Greco
+1  A: 

You must check first if StreetAdditional is null.

Try

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

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

Dario
+2  A: 

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

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>()
Vasu Balakrishnan
Looks somewhat strange because a `null`-objects seems to be able to invoke member functions.
Dario
Upvote for the use of the ?? operator. That's what I would have suggested as well. If you feel that the extension method makes the query look strange you could do without the extension method.
Jeroen Huinink
+1  A: 

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>()
Yuliy
+1  A: 

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).

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

David B
A: 

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