Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq orderby计算_C#_Sql_Linq_Sorting - Fatal编程技术网

C# Linq orderby计算

C# Linq orderby计算,c#,sql,linq,sorting,C#,Sql,Linq,Sorting,我刚才为一个工具构建了一个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

我刚才为一个工具构建了一个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();

有什么想法吗?

不同的
不保留排序。这是你的问题


您可以像在SQL中那样执行
分组,以模仿
不同的
,并在服务器端执行所有操作

您的原始查询正在执行分组-您的LINQ查询没有执行分组。@LanFeusT:为了可读性,我将使用一个“let”子句,在其中计算权重,然后按“权重”排序。它不会改变性能或输出,只是带有公式的order by子句伤害了我的眼睛:-P@Tipx绝对是个好主意!^^谢谢。事实上,按分组不会“模仿不同的”,这将是他的问题的正确翻译,这样他的不同的人就不会再模仿他的分组了!;-)有没有办法将分组转换回列表?由于c.ManHoursEst(c.ManHoursEst*c.TestingStatus.Value/100)对b组进行
操作后会将其转换为一个列表?@LanFeusT:当您正确查询时,您只需说
SelectMany(g=>g).ToList()
。但你的质疑显然是错误的。你为什么要按这个来分组?好吧,我试着按我的品牌。Id和订单的权重来分组。但很明显,我在那里做错了xD
var brands=(从b.YearId==YearId&&c.IsPunted==false)让权重=c.ManHoursEst-(c.ManHoursEst*c.testingstatus.Value/100)按b组按b.MilestoneId递减加权排序。选择many(g=>g)。ToList()
这给了我一个包含大量重复项且未正确排序的列表。我会将此答案标记为正确,因为这肯定是我的问题。我无法获得正确的查询,而只是使用了我的SQL查询并将其转储到存储过程中,这使一切变得更加简单。感谢您的帮助!