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

前端 未结 9 1036
别那么骄傲
别那么骄傲 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 02:17

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

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

提交回复
热议问题