<p>I started playing around with Linq today and ran into a problem I couldn't find an answer to. I was querying a simple SQL Server database that had some employee records. One of the fields is the full name (cn). I thought it would be interesting to group by the first name by splitting the full name at the first space. I tried</p>

<pre><code>group by person.cn.Split(separators)[0]
</code></pre>

<p>but ran into a lengthy runtime exception (looked a lot like a C++ template instantiation error).</p>

<p>Then I tried grouping by a few letters of the first name:</p>

<pre><code>group by person.cn.Substring(0,5)
</code></pre>

<p>and that worked fine but is not what I want.</p>

<p>I'm wondering about two things:</p>

<ul>
<li>Why does the first example not work when it looks so close to the second?</li>
<li>Knowing that behind the scenes it's SQL stuff going on, what's a good way to do this kind of thing efficiently</li>
</ul>

<p>Thanks,</p>

<p>Andrew</p>

Split has no translation into SQL.
So, how to do this string manipulation without split?  Cheat like hell (untested):
```
```
string oneSpace = " ";
string fiftySpace = "                                                  ";

var query = 
  from person in db.Persons
  let lastname = person.cn.Replace(oneSpace, fiftySpace).SubString(0, 50).Trim()
  group person by lastname into g
  select new { Key = g.Key, Count = g.Count };
```
```