Linq顺序计算

本文关键字:计算 顺序 Linq | 更新日期: 2023-09-27 18:09:04

我有一个SQL查询,我建立了一个工具前一段时间,我在MVC和使用LINQ实体重新制作工具。

我似乎不知道如何通过按工时和测试值对我的汽车进行加权来排序我的品牌列表。

下面是我在旧工具中的SQL查询:

    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

我已经尝试过了,但无论我把降序或升序实际上并没有改变列表,它保持按Id排序:

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 && c.IsPunted == false)
            orderby (c.ManHoursEst - (c.ManHoursEst * c.TestingStatu.Value / 100)) descending 
            select b).Distinct().ToList();

将感谢帮助这个转换!

谢谢。编辑:

我现在正试着让order by和group by正确工作。下面的查询列出了大量的重复项,并且没有正确排序,因为我认为我的权重没有正确完成。

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 && c.IsPunted == false)
            let weighting = c.ManHoursEst - (c.ManHoursEst * c.TestingStatu.Value / 100)
            orderby weighting descending 
            group b by b.Id).SelectMany(x=>x).ToList();

任何想法?

Linq顺序计算

Distinct不保留排序。那是你的问题。

您可以在SQL中执行group by,以模仿Distinct并执行所有服务器端。