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

前端 未结 9 990
别那么骄傲
别那么骄傲 2020-12-14 01:23

Here is the query

from a in this._addresses
where a.Street.Contains(street) || a.StreetAdditional.Contains(streetAdditional)
select a).ToList
(
9条回答
  •  天涯浪人
    2020-12-14 01:56

    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.

提交回复
热议问题