title: A lambda expression with a statement body cannot be converted to an expression tree(Lambda and Linq)
body: <p>Search A lot but did not find any suitable solution for me. I have introduced a variable in <code>lambda expression</code> and that's why showing this error message . Moreover I can solve the problem writing "SQL Like Linq Query" but this is not my expectation..My expectation is to introduce variable in "Linq Using Lambda Expression". Is it Possible or not??</p>
  
  <blockquote>
    <p>A lambda expression with a statement body cannot be converted to an expression tree.</p>
  </blockquote>
  
  <p>Here is my code: </p>
  
  <pre><code>IQueryable&lt;IGrouping&lt;int, AnimalFood&gt;&gt; animalFoods = db.AnimalFoods.GroupBy(x =&gt; x.FoodId);
  IQueryable&lt;FoodSummaryViewModel&gt; foodSummaryViewModel = animalFoods.Select(g =&gt;
  {
      var animalFood = g.FirstOrDefault();
      return new FoodSummaryViewModel()
      {
          FoodName = animalFood.Food.FoodName,
          FoodPrice = animalFood.Food.UnitPrice,
          TotalFoodQuantity = g.Sum(x =&gt; x.Animal.AnimalQuantity * x.FoodQuantity),
          TotalPrice = g.Sum(x =&gt; x.Animal.AnimalQuantity * x.FoodQuantity) * animalFood.Food.UnitPrice
      };
  });
  return foodSummaryViewModel.ToList();
  </code></pre>