title: LINQ SQL query check if a object field is not null
body: <p>I am trying write a SQL query that filters a gridview by the fields that are entered.  There are four fields, title, firstname, surname and Company.Name.</p>
  
  <p>The first three are fine as they are never null but the fourth can be null.  The following LINQ Query works just fine:</p>
  
  <pre><code>var listofclients = from client in allcients
                      where client.Title.ToLower().Contains(titletxtbox.Text.Trim().ToLower())
                      where client.Firstname.ToLower().Contains(firstnametxtbox.Text.Trim().ToLower())
                      where client.Surname.ToLower().Contains(surnametxtbox.Text.Trim().ToLower())
                      orderby client.Name
  </code></pre>
  
  <p>But when I try and put a filter into it for the company I will get an error at runtime when the company is null</p>
  
  <pre><code>var listofclients = from client in allcients
                      where client.Title.ToLower().Contains(titletxtbox.Text.Trim().ToLower())
                      where client.Firstname.ToLower().Contains(firstnametxtbox.Text.Trim().ToLower())
                      where client.Surname.ToLower().Contains(surnametxtbox.Text.Trim().ToLower())
                      where client.Company.Name.ToLower().Contains(companynametxtbox.Text.Trim().ToLower())
                      orderby client.Name
  </code></pre>
  
  <p>What I would like to know, is there a way to build the query so that it will only filter when the client.Company field is not null.</p>
  
  <p>Also am I vulnerable to SQL injection or the like when I pull directly from the textbox fields like this.  I know in this case it is not connected to the DB but if it was could they do a drop.  Or even if it is not connected to the db could they fiddle with the objects in the list?</p>
  
  <p>Thanks</p>
  
  <p>Jon Hawkins</p>