【Bug】linq:如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。(EF Core)

原报错代码:

        public WebResponseContent GetNewProductProductionSchedulingList(ApiNewProductProductionSchedulingQueryInput apiNewProductProductionSchedulingQueryInput)
        {
            WebResponseContent<List<ApiNewProductProductionSchedulingOutPut>> result = new WebResponseContent<List<ApiNewProductProductionSchedulingOutPut>>();
            var resultTemp = repository.FindAsIQueryable(f => f.IsDeleted == 0).Select(f => new ApiNewProductProductionSchedulingOutPut
            {
                Id=f.Id,
                ManagementNo = f.ManagementNo,
                CreateTime = f.CreateTime,
                ModifyTime = f.ModifyTime,
                State = f.State,
                CreatorCode = f.CreatorCode
            }).Distinct();
            //条件
            if (apiNewProductProductionSchedulingQueryInput.ManagementNo != null)//管理编号
            {
                resultTemp = resultTemp.Where(f => f.ManagementNo == apiNewProductProductionSchedulingQueryInput.ManagementNo);
            }
            if (apiNewProductProductionSchedulingQueryInput.State != null)//排产状态(0=待排产1=排产中2=已完成)
            {
                resultTemp = resultTemp.Where(f => f.State == apiNewProductProductionSchedulingQueryInput.State);
            }
            if (apiNewProductProductionSchedulingQueryInput.CreatorCode != null)//员工
            {
                resultTemp = resultTemp.Where(f => f.CreatorCode == apiNewProductProductionSchedulingQueryInput.CreatorCode);
            }
            return result.OK("获取成功", resultTemp.ToList());
        }

原ef翻译:

SELECT DISTINCT [b].[management_no] AS [ManagementNo], [b].[create_time] AS [CreateTime], [b].[modify_time] AS [ModifyTime], [b].[state] AS [State], [b].[creator_code] AS [CreatorCode]
FROM [biz_new_product_production_scheduling] AS [b]
WHERE (([b].[is_deleted] = 0) AND ([b].[is_deleted] = 0)) AND ([b].[management_no] = @__apiNewProductProductionSchedulingQueryInput_ManagementNo_0)
ORDER BY (SELECT 1)
OFFSET 0 ROWS FETCH NEXT @__p_1 ROWS ONLY

修改后代码:

  public WebResponseContent GetNewProductProductionSchedulingList(ApiNewProductProductionSchedulingQueryInput apiNewProductProductionSchedulingQueryInput)
        {
            var pageIndex = apiNewProductProductionSchedulingQueryInput.PageIndex <= 0 ? 1 : apiNewProductProductionSchedulingQueryInput.PageIndex;
            var pagesize = apiNewProductProductionSchedulingQueryInput.PageSize <= 0 ? 10 : apiNewProductProductionSchedulingQueryInput.PageSize;

            WebResponseContent<List<ApiNewProductProductionSchedulingOutPut>> result = new WebResponseContent<List<ApiNewProductProductionSchedulingOutPut>>();
            var resultTemp = repository.FindAsIQueryable(f => f.IsDeleted == 0).Select(f => new ApiNewProductProductionSchedulingOutPut
            {
                ManagementNo = f.ManagementNo,
                CreateTime = f.CreateTime,
                ModifyTime = f.ModifyTime,
                State = f.State,
                CreatorCode = f.CreatorCode
            });
            //条件
            if (apiNewProductProductionSchedulingQueryInput.ManagementNo != null)//管理编号
            {
                resultTemp = resultTemp.Where(f => f.ManagementNo == apiNewProductProductionSchedulingQueryInput.ManagementNo);
            }
            if (apiNewProductProductionSchedulingQueryInput.State != null)//排产状态
            {
                resultTemp = resultTemp.Where(f => f.State == apiNewProductProductionSchedulingQueryInput.State);
            }
            if (apiNewProductProductionSchedulingQueryInput.CreatorCode != null)//员工
            {
                resultTemp = resultTemp.Where(f => f.CreatorCode == apiNewProductProductionSchedulingQueryInput.CreatorCode);
            }

            resultTemp = resultTemp.Distinct().OrderBy(f => f.CreateTime).Skip((pageIndex - 1) * pagesize).Take(pagesize);
            return result.OK("获取成功", resultTemp.ToList());
        }

修改后ef翻译:

SELECT [t].[management_no] AS [ManagementNo], [t].[create_time] AS [CreateTime], [t].[modify_time] AS [ModifyTime], [t].[state] AS [State], [t].[creator_code] AS [CreatorCode]
FROM (
    SELECT DISTINCT [b].[management_no], [b].[create_time], [b].[modify_time], [b].[state], [b].[creator_code]
    FROM [biz_new_product_production_scheduling] AS [b]
    WHERE (([b].[is_deleted] = 0) AND ([b].[is_deleted] = 0)) AND ([b].[management_no] = @__apiNewProductProductionSchedulingQueryInput_ManagementNo_0)
) AS [t]
ORDER BY [t].[create_time]
OFFSET 0 ROWS FETCH NEXT @__p_1 ROWS ONLY```

值得注意的是无论ORDER BY多少字段 被翻译的都只有最后一个ORDER
BY的字段,(有机会确认下)要想正确被翻译应该用ThenByDescending() 和ThenBy()

处理Bug的具体步骤

  1. 重现BUG
  2. 确定发生BUG的过程时都做了哪些动作
  3. 桌面检查:把自己的脑子作为CPU,运行从开始操作到出现BUG的操作、处理的数据,分析出现问题的原因
  4. 调试:确定问题出现的原因
  5. 确定解决方案
  6. 修复BUG
  7. 回归测试:确定已BUG已修复并没引入新问题

没有粉丝,没有评论,甚至连读者都没有。因为自己写的烂,因为自己水平有限,所以自己的作品一经发出就石沉大海了,得不到任何的回信,就好像自己写的东西是给自己看的。自己的作品怎么看都好像不够完美,全身的毛病,所以读者不喜欢也很正常对吧!

其实最让我开心的是有些读者们夸我文笔不错,或许是有人第一次这样夸我的缘故吧!又或许是我写的东西没什么人认可,我感受到了一种前所未有的激动,对于未来的憧憬,对自己写下去的动力。

因为有你们,我的写作之路才不孤独,因为有你们,我才能坚持到现在,如果有一天我真的写出圈了,或许我们可以整个交流会之类的,到时候促膝长谈。

“书山有路勤为径,学海无涯苦作舟。”写作苦是苦了点,整得我差点就想放弃了,不过还好有你们。我也不急着成功,因为没有个几十年如一日的坚持,哪能轻易成功?

茫茫文海无人意,天下有君识吾心。文海很孤独,但是有诸君,便足以令我至千里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软泡芙

给爷鞠躬!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值