Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

枚举类型 使用string 存入数据库,在分组聚合中使用枚举时,生成的sql 是按int 值进行比对,期望按 string进行比对。 #1727

Closed
Roy-se7en opened this issue Jan 26, 2024 · 1 comment

Comments

@Roy-se7en
Copy link

问题描述及重现代码:

枚举类型 使用string 存入数据库,在分组聚合中使用枚举时,生成的sql 是按int 值进行比对,期望按 string进行比对。

Entity关键字段

  /// <summary>
  /// 检验结果
  /// </summary>
  [Column(MapType = typeof(string), StringLength = 50)]
  public InspectionResultTypeEnum InspectionResultType { get; set; }

枚举

    public enum InspectionResultTypeEnum : int
    {
        /// <summary>
        /// 合格
        /// </summary>
        [Description("合格")]
        Qualified = 20,

        /// <summary>
        /// 不合格
        /// </summary>
        [Description("不合格")]
        UnQualified = 30,

        /// <summary>
        /// 特采
        /// </summary>
        [Description("特采")]
        Special = 40,
    }

执行查询的代码

            return await _produceOrderBomQualityInspectionRepository
                 .Where(t => t.ProduceOrderId.HasValue && produceOrderIds.Contains(t.ProduceOrderId.Value))
                 .GroupBy(t => t.ProduceOrderId.Value)
                 .ToListAsync(t => new PartQualityInspectQuantityOfProduceOrderDto
                 {
                     ProduceOrderId = t.Key,
                     QualifiedQuantity = t.Sum((t.Value.InspectionResultType == InspectionResultTypeEnum.Qualified || t.Value.InspectionResultType == InspectionResultTypeEnum.Special) ? t.Value.InspectionQuantity : 0),
                     UnQualifiedQuantity = t.Sum(t.Value.InspectionResultType == InspectionResultTypeEnum.UnQualified ? t.Value.InspectionQuantity : 0)
                 });

实际的SQL

SELECT a.`ProduceOrderId` as1, sum(case when (a.`InspectionResultType` = 20 OR a.`InspectionResultType` = 40) then a.`InspectionQuantity` else 0 end) as2, sum(case when a.`InspectionResultType` = 30 then a.`InspectionQuantity` else 0 end) as3
 FROM `produce_order_bom_quality_inspection` a
 WHERE (a.`ProduceOrderId` IS NOT NULL AND ((a.`ProduceOrderId`) in (110,112))) AND (a.`IsDeleted` = 0)
 GROUP BY a.`ProduceOrderId`

期望的SQL

 SELECT a.`ProduceOrderId` as1, sum(case when (a.`InspectionResultType` = 'Qualified' OR a.`InspectionResultType` = 'Special') then a.`InspectionQuantity` else 0 end) as2, sum(case when a.`InspectionResultType` = 'UnQualified' then a.`InspectionQuantity` else 0 end) as3
 FROM `produce_order_bom_quality_inspection` a
 WHERE (a.`ProduceOrderId` IS NOT NULL AND ((a.`ProduceOrderId`) in (110,112))) AND (a.`IsDeleted` = 0)
 GROUP BY a.`ProduceOrderId`

数据库版本

Mysql 8.0.32

安装的Nuget包

3.2.808

.net framework/. net core

.NET7

@2881099
Copy link
Collaborator

2881099 commented Feb 1, 2024

本次更新内容撤销了,特殊情况使用【自定义函数】解析临时解决一下。

2881099 added a commit that referenced this issue Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants