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

前端 未结 9 909
别那么骄傲
别那么骄傲 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<Address>()
    

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

    0 讨论(0)
  • 2020-12-14 02:21

    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>
    

    ()

    0 讨论(0)
  • 2020-12-14 02:22
    from a in this._addresses
    where a.Street.Contains(street) || (a.StreetAdditional != null && a.StreetAdditional.Contains(streetAdditional)
    select a).ToList<Address>()
    
    0 讨论(0)
提交回复
热议问题