title: Cannot be translated into a LINQ to Entities store expression
body: <p>I am relatively new to LINQ-to-Entities, but use LINQ-to-SQL a lot.</p>
  
  <p>I am using Visual Studio 2013 with Entity Framework 6 and MVC 5.</p>
  
  <p>The biggest difference between the two is that Linq2SQL has the ability to perform conversions inside the <code>SELECT</code> query itself whereas LINQ2Entities is not as forgiving and must have the right conversion in place before executing the LINQ query. Therefore, I am getting the error: </p>
  
  <blockquote>
    <p>The specified method 'System.Decimal ConvertToDecimal(Byte)' on the type 'BillYeagerDB.EdmxExtensionMethods' cannot be translated into a LINQ to Entities store expression.</p>
  </blockquote>
  
  <p>After doing much research, especially on Stack Overflow with this question, I discovered a link (<a href="https://stackoverflow.com/questions/5971521/linq-to-entities-does-not-recognize-the-method-double-parsesystem-string-met">LINQ to Entities does not recognize the method &#39;Double Parse(System.String)&#39; method, and this method cannot be translated into a store expression</a>), but he was working with the <code>ObjectContext</code> and I am working with the <code>DbContext</code>.</p>
  
  <p>I'm also sure it will work for me, but I think I'm just designing the extension method incorrectly (which gives me the above error). Note that this specific issue is with the <code>AvgRating</code> variable in the LINQ query. Once I can get this to work, I can do the same type of fixing for any other conversions. Note that <code>AvgRating</code> is defined as type <code>Decimal</code> and <code>a.Rating.RatingValue</code> is defined as type <code>Byte</code>.</p>
  
  <p>If somebody can straighten me out on this, I would greatly appreciate it.</p>
  
  <p>Here is my code. I'm trying to use the following query, which I know won't work (as mentioned before) because of the conversion issue.</p>
  
  <p><strong>Original LINQ Query:</strong></p>
  
  <pre><code>namespace BillYeagerDB
  {
      public class BillYeagerDB
      {
          public async Task&lt;List&lt;RestaurantList&gt;&gt; GetRestaurantListAsync()
          {
              try
              {
                  using (BillYeagerEntities DbContext = new BillYeagerEntities())
                  {
                      DbContext.Database.Connection.Open();
  
                      var restaurants = await DbContext.Restaurants.GroupBy(g =&gt; g).Select(s =&gt;
                          new RestaurantList()
                          {
                              Name = s.Key.Name,
                              City = s.Key.City,
                              Phone = s.Key.Phone,
                              AvgRating = s.Average(a =&gt; Convert.ToDecimal(a.Rating.RatingValue)),
                              NbrOfPeopleRating = s.Distinct().Count(c =&gt; Convert.ToBoolean(c.RatingId)),
                              Id = s.Key.Id
                          }).ToListAsync();
  
                      return restaurants;
                  }
              }
              catch (Exception)
              {
                  throw;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Update I needed to do to my EDMX file</strong></p>
  
  <pre><code>&lt;edmx:ConceptualModels&gt;
        &lt;Schema Namespace="BillYeagerModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm"&gt;
          &lt;Function Name="ParseDecimal" ReturnType="Edm.Decimal"&gt; 
              &lt;Parameter Name="bytevalue" Type="Edm.Byte" /&gt; 
              &lt;DefiningExpression&gt; 
                  cast(bytevalue as Edm.Decimal)
              &lt;/DefiningExpression&gt; 
          &lt;/Function&gt;
  </code></pre>
  
  <p><strong>C# extension method which is a class on the root of my project - not inside my EDMX</strong></p>
  
  <pre><code>namespace BillYeagerDB
  {
      public partial class EdmxExtensionMethods : DbContext
      {
          [DbFunctionAttribute("BillYeagerDB", "ParseDecimal")]
          public static Decimal ParseDecimal(byte bytevalue)
          {
              return Convert.ToDecimal(bytevalue);
          }
      }
  }
  </code></pre>
  
  <p><strong>Updated Linq query - note no design time compile errors and project compiles successfully</strong></p>
  
  <pre><code>namespace BillYeagerDB
  {
      public class BillYeagerDB
      {
          public async Task&lt;List&lt;RestaurantList&gt;&gt; GetRestaurantListAsync()
          {
              try
              {
                  using (BillYeagerEntities DbContext = new BillYeagerEntities())
                  {
                      DbContext.Database.Connection.Open();
  
                      var restaurants = await DbContext.Restaurants.GroupBy(g =&gt; g).Select(s =&gt;
                          new RestaurantList()
                          {
                              Name = s.Key.Name,
                              City = s.Key.City,
                              Phone = s.Key.Phone,
                              AvgRating = s.Average(a =&gt; EdmxExtensionMethods.ConvertToDecimal(a.Rating.RatingValue)),
                              NbrOfPeopleRating = s.Distinct().Count(c =&gt; Convert.ToBoolean(c.RatingId)),
                              Id = s.Key.Id
                          }).ToListAsync();
  
                      return restaurants;
                  }
              }
              catch (Exception)
              {
                  throw;
              }
          }
      }
  }
  </code></pre>