title: Using LINQ how do I have a grouping by a "calculated field"
body: <p>I am using LINQ to EF and have the following LINQ query:</p>
  
  <pre><code>var results = (from x in ctx.Items
                 group x by x.Year into decades
                 orderby decades.Count() descending
                 select new { Decade = decades.Key, DecadeCount = decades.Count() });
  </code></pre>
  
  <p>So this kind of gets me to where I want to be, in that I get the items broken down by year and a count of items in that year.  (i.e. 2001 - 10, 1975 - 15, 2005 - 5, 1976 - 1)  The thing I really want to do though is to break them down by decade (i.e. 2000s - 15, 1970s - 16).</p>
  
  <p>How does one have a "calculated field" in the "by" part of the group clause for a Linq statement.  I think what I want is basically something like:</p>
  
  <pre><code>var results = (from x in ctx.Items
                 group x by (x =&gt; x.Year.Value.ToString().Substring(0, 3) + "0s") into decades
                 orderby decades.Count() descending
                 select new { Decade = decades.Key, DecadeCount = decades.Count() });
  </code></pre>
  
  <p>Or more generally the syntax so that I can do some more complicated evaluation/calculation to do the group by on.  Any ideas?</p>
  
  <p>EDIT (update):</p>
  
  <p>(x => x.Year.Value.ToString().Substring(0, 3) + "0s") - Doesn't Work - "LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."</p>
  
  <p>(x.Year / 10 * 10) - Functionally works (thank you) - the only "problem" is that the 's' is not on the end (i.e. 1970 vs. 1970s)</p>
  
  <p>Is there anyway to put a function in the by clause?  i.e. group x by this.ManipulateYear(x.Year) into decades ... or ... x => x.Year.Value.ToString().Substring(0,3) + "0s" ??  It would be nice to have some technique (such as calling a function or using a lambda expression) so that I can cover any case that I can think of.</p>
  
  <p>Thanks again for everyone's help on this.</p>