title: Linq orderby calculation
body: <p>I have an SQL query that I built for a tool a while ago and I'm remaking the tool in MVC and using LINQ to Entities.</p>
  
  <p>I can't seem to figure out how to sort my list of Brands by weighting my Cars by man hours and their testing value.</p>
  
  <p>Here's the SQL query I had in the old tool:</p>
  
  <pre><code>    SELECT Brand.ID, SUM(Car.EstManHours) - SUM(Car.EstManHours) * CAST(AVG(1.00 * TestingStatus.Value) AS DECIMAL(9 , 2)) / 100 AS Weighting
  FROM TestingStatus INNER JOIN Car ON TestingStatus.ID = Car.StatusID
      INNER JOIN Team ON Car.TeamID = Team.TeamID 
      RIGHT OUTER JOIN Brand 
      LEFT OUTER JOIN SubCategory ON Brand.ID = SubCategory.BrandID ON Car.SubCategoryID = SubCategory.ID 
  WHERE (Car.IsPunted == 'False')
  GROUP BY Brand.YearID, Brand.FeatID
  HAVING (Brand.YearID = @BrandYearID)
  ORDER BY Weighting DESC
  </code></pre>
  
  <p>I've tried this, but whether I put descending or ascending the order doesn't actually change in the list, it keeps the sorting by Id:</p>
  
  <pre><code>var brands = (from b in _context.Brands
              join s in _context.SubCategorys on f.Id equals s.BrandId
              join c in _context.Cars on s.Id equals c.SubCategoryId
              where (f.YearId == yearId &amp;&amp; c.IsPunted == false)
              orderby (c.ManHoursEst - (c.ManHoursEst * c.TestingStatu.Value / 100)) descending 
              select b).Distinct().ToList();
  </code></pre>
  
  <p>Would appreciate help on this conversion!</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>I'm now trying to get the order by and group by to work correctly.
  The following query is listing tons of duplicates and not ordering properly as I don't think my weighting is done correctly.</p>
  
  <pre><code>var brands = (from b in _context.Brands
              join s in _context.SubCategorys on f.Id equals s.BrandId
              join c in _context.Cars on s.Id equals c.SubCategoryId
              where (f.YearId == yearId &amp;&amp; c.IsPunted == false)
              let weighting = c.ManHoursEst - (c.ManHoursEst * c.TestingStatu.Value / 100)
              orderby weighting descending 
              group b by b.Id).SelectMany(x=&gt;x).ToList();
  </code></pre>
  
  <p>Any ideas?</p>