gpt4 book ai didi

c# - Linq orderby计算

转载 作者:太空狗 更新时间:2023-10-30 01:25:11 24 4
gpt4 key购买 nike

我有一个之前为工具构建的 SQL 查询,我正在 MVC 中重新制作该工具并使用 LINQ to Entities。

我似乎无法弄清楚如何通过按工时和测试值对我的汽车进行加权来对我的品牌列表进行排序。

这是我在旧工具中的 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();

非常感谢您对此转换的帮助!

谢谢。

编辑:

我现在正在尝试让排序依据和分组依据正常工作。以下查询列出了大量重复项并且没有正确排序,因为我认为我的权重没有正确完成。

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();

有什么想法吗?

最佳答案

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

您可以像在 SQL 中一样执行 group by 来模仿 Distinct 并在服务器端执行所有操作。

关于c# - Linq orderby计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7488833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com