title: Behaviour of string.Trim() in an Entity Framework query
body: <p>I am trying to copy a list of names into a SQL Server table, for which I have an Entity Framework project setup for.</p>
  
  <p>The list of names has duplicate values, and several have spaces on the end of them.  I wish to only insert names that aren't already in the table, as well as trim the spaces from the end of them.  Seems fairly simple, right?</p>
  
  <p>My solution was this:</p>
  
  <pre><code>if (!context.Names.Any(n =&gt; n.Value == nameToCopy.Trim())
    context.Names.Add(nameToCopy.Trim())
  </code></pre>
  
  <p>NB. this isn't my exact code, just an example, so no need to mention that I'm trimming twice!</p>
  
  <p>To my surprise, the above doesn't work the way I expected.  I found whilst profiling the above statement, that the <code>if (!context.Names.Any(n =&gt; n.Value == nameToCopy.Trim())</code> doesn't actually query for a trimmed version of nameToCopy - several of the queried names had spaces on the end of them.</p>
  
  <p>If, however, I do the below instead, it works as expected:</p>
  
  <pre><code>string trimmedName = nameToCopy.Trim()
  if (!context.Names.Any(n =&gt; n.Value == trimmedName)
        context.Names.Add(trimmedName)
  </code></pre>
  
  <p>Can anyone explain why the first solution doesn't use a trimmed version of the string in the database query?</p>
  
  <p>Thanks</p>