生成sql语句错误 返回

SqlSugar 沟通中
6 615

var orderDetailModel = await postalRepository.Context.Queryable<OrdersEntity>().LeftJoin<PaymentEntity>((o, p) => o.Id == p.OrderId)

.Where((o, p) => o.Id == orderId)

.Select((o, p) => new Res_QueryOrderDetail

{

    OrderNo = o.OrderNo,

    PayType = o.PayType,

    ExpressNo = o.ExpressNo,

    ApplyTime = o.CreateTime,

    ServingFee = o.ServingFee,

    InsureTime = o.InsureTime,

    DeliverCity = o.DeliverCity,

    OrderStatus = o.OrderStatus,

    DeliverName = o.DeliverName,

    ReceiverCity = o.ReceiverCity,

    DeliverAddress = SqlFunc.MergeString(o.DeliverProvince, o.DeliverCity, o.DeliverDistrict, o.DeliverStreet),

    ReceiverAddress = SqlFunc.MergeString(o.ReceiverProvince, o.ReceiverCity, o.ReceiverDistrict, o.ReceiverStreet),

    Minute = SqlFunc.IIF(o.OrderStatus == (int)OrderStatusEnum.WaitPay, (p.CreateTime.AddMinutes(cancelPayOptions.Value.DelayTime) - DateTime.Now).Minutes,0),

    //Minute = o.OrderStatus == (int)OrderStatusEnum.WaitPay ? (p.CreateTime.AddMinutes(cancelPayOptions.Value.DelayTime) - DateTime.Now).Minutes : 0,

    //Second = o.OrderStatus == (int)OrderStatusEnum.WaitPay ? (p.CreateTime.AddMinutes(cancelPayOptions.Value.DelayTime) - DateTime.Now).Seconds : 0,

    InsuranceFileAddress = o.InsuranceFileAddress,

    WorkPrice = o.InsurancePrice - o.ServingFee,

    InsuranceQuantity = o.InsuranceQuantity,

    ExpressCompany = o.ExpressCompany,

    ReceiverMobile = o.ReceiverMobile,

    InsuranceMoney = o.InsuranceMoney,

    InsurancePrice = o.InsurancePrice,

    DeliverMobile = o.DeliverMobile,

    InsuranceType = o.InsuranceType,

    InsuranceCode = o.InsuranceCode,

    ReceiverName = o.ReceiverName,

})

.FirstAsync();


这是我的语句

为什么生成的SQL语句是这样的SELECT  TOP 1  [o].[OrderNo] AS [OrderNo] , [o].[PayType] AS [PayType] , [o].[ExpressNo] AS [ExpressNo] , [o].[CreateTime] AS [ApplyTime] , [o].[ServingFee] AS [ServingFee] , [o].[InsureTime] AS [InsureTime] , [o].[DeliverCity] AS [DeliverCity] , [o].[OrderStatus] AS [OrderStatus] , [o].[DeliverName] AS [DeliverName] , [o].[ReceiverCity] AS [ReceiverCity] , [o].[DeliverProvince]+[o].[DeliverCity]+[o].[DeliverDistrict]+[o].[DeliverStreet] AS [DeliverAddress] , [o].[ReceiverProvince]+[o].[ReceiverCity]+[o].[ReceiverDistrict]+[o].[ReceiverStreet] AS [ReceiverAddress] , DATEDIFF(Minute, '2025/9/3 16:38:08' , (DATEADD(Minute,10,[p].[CreateTime])) ) , ( CASE  WHEN ( [o].[OrderStatus] = 1 ) THEN Result  ELSE 0 END ) AS [Minute] , [o].[InsuranceFileAddress] AS [InsuranceFileAddress] , ( [o].[InsurancePrice] - [o].[ServingFee] ) AS [WorkPrice] , [o].[InsuranceQuantity] AS [InsuranceQuantity] , [o].[ExpressCompany] AS [ExpressCompany] , [o].[ReceiverMobile] AS [ReceiverMobile] , [o].[InsuranceMoney] AS [InsuranceMoney] , [o].[InsurancePrice] AS [InsurancePrice] , [o].[DeliverMobile] AS [DeliverMobile] , [o].[InsuranceType] AS [InsuranceType] , [o].[InsuranceCode] AS [InsuranceCode] , [o].[ReceiverName] AS [ReceiverName]  FROM [JBHP_Orders] [o] Left JOIN [JBHP_Payment] [p] ON ( [o].[Id] = [p].[OrderId] )   WHERE ( [o].[Id] = 1962776380424982528 )  AND ( [o].[DeleteMark] = 0 )

报错result列名无效


热忱回答6

  • 是最新版本吗,是的话我这边验证一下。

    0 回复
  • @fate sta:用的5.1.4.198

    0 回复
  • 这是我的语句

    var orderDetailModel = await postalRepository.Context.Queryable<OrdersEntity>().LeftJoin<PaymentEntity>((o, p) => o.Id == p.OrderId)

        .Where((o, p) => o.Id == orderId)

        .Select((o, p) => new Res_QueryOrderDetail

        {

            OrderNo = o.OrderNo,

            PayType = o.PayType,

            ExpressNo = o.ExpressNo,

            ApplyTime = o.CreateTime,

            ServingFee = o.ServingFee,

            InsureTime = o.InsureTime,

            DeliverCity = o.DeliverCity,

            OrderStatus = o.OrderStatus,

            DeliverName = o.DeliverName,

            ReceiverCity = o.ReceiverCity,

            DeliverAddress = SqlFunc.MergeString(o.DeliverProvince, o.DeliverCity, o.DeliverDistrict, o.DeliverStreet),

            ReceiverAddress = SqlFunc.MergeString(o.ReceiverProvince, o.ReceiverCity, o.ReceiverDistrict, o.ReceiverStreet),

            //Minute = o.OrderStatus == (int)OrderStatusEnum.WaitPay &&

            //SqlFunc.DateDiff(DateType.Second, DateTime.Now, SqlFunc.DateAdd(p.CreateTime, cancelPayOptions.Value.DelayTime, DateType.Minute)) / 60 > 0 ?

            //SqlFunc.DateDiff(DateType.Second, DateTime.Now, SqlFunc.DateAdd(p.CreateTime, cancelPayOptions.Value.DelayTime, DateType.Minute)) / 60 : 0,

            //Second = o.OrderStatus ==

            //(int)OrderStatusEnum.WaitPay &&

            //SqlFunc.DateDiff(DateType.Second, DateTime.Now, SqlFunc.DateAdd(p.CreateTime, cancelPayOptions.Value.DelayTime, DateType.Minute)) % 60 > 0 ?

            //SqlFunc.DateDiff(DateType.Second, DateTime.Now, SqlFunc.DateAdd(p.CreateTime, cancelPayOptions.Value.DelayTime, DateType.Minute)) % 60 : 0,

            Minute = o.OrderStatus == (int)OrderStatusEnum.WaitPay ? (p.CreateTime.AddMinutes(cancelPayOptions.Value.DelayTime) - DateTime.Now).Minutes : 0,

            Second = o.OrderStatus == (int)OrderStatusEnum.WaitPay ? (p.CreateTime.AddMinutes(cancelPayOptions.Value.DelayTime) - DateTime.Now).Seconds : 0,

            InsuranceFileAddress = o.InsuranceFileAddress,

            WorkPrice = o.InsurancePrice - o.ServingFee,

            InsuranceQuantity = o.InsuranceQuantity,

            ExpressCompany = o.ExpressCompany,

            ReceiverMobile = o.ReceiverMobile,

            InsuranceMoney = o.InsuranceMoney,

            InsurancePrice = o.InsurancePrice,

            DeliverMobile = o.DeliverMobile,

            InsuranceType = o.InsuranceType,

            InsuranceCode = o.InsuranceCode,

            ReceiverName = o.ReceiverName,

        })

        .FirstAsync();

    生成的sql是



    SELECT  TOP 1  [o].[OrderNo] AS [OrderNo] , [o].[PayType] AS [PayType] , [o].[ExpressNo] AS [ExpressNo] , 

    [o].[CreateTime] AS [ApplyTime] , [o].[ServingFee] AS [ServingFee] , [o].[InsureTime] AS [InsureTime] , 

    [o].[DeliverCity] AS [DeliverCity] , [o].[OrderStatus] AS [OrderStatus] , [o].[DeliverName] AS [DeliverName] ,

     [o].[ReceiverCity] AS [ReceiverCity] , [o].[DeliverProvince]+[o].[DeliverCity]+[o].[DeliverDistrict]+[o].[DeliverStreet] AS [DeliverAddress] ,

     [o].[ReceiverProvince]+[o].[ReceiverCity]+[o].[ReceiverDistrict]+[o].[ReceiverStreet] AS [ReceiverAddress] ,  

    DATEDIFF(Minute, '2025/9/4 9:49:31' , (DATEADD(Minute,10,[p].[CreateTime])) )

     ( CASE  WHEN ( [o].[OrderStatus] = 1 ) THEN Result  ELSE 0 END ) AS [Minute] ,  

    DATEDIFF(Second, '2025/9/4 9:49:31' , (DATEADD(Minute,10,[p].[CreateTime])) ) 

    ( CASE  WHEN ( [o].[OrderStatus] = 1 ) THEN Result  ELSE 0 END ) AS [Second] , 

    [o].[InsuranceFileAddress] AS [InsuranceFileAddress] , ( [o].[InsurancePrice] - [o].[ServingFee] ) AS [WorkPrice] , 

    [o].[InsuranceQuantity] AS [InsuranceQuantity] , [o].[ExpressCompany] AS [ExpressCompany] , 

    [o].[ReceiverMobile] AS [ReceiverMobile] , [o].[InsuranceMoney] AS [InsuranceMoney] ,

     [o].[InsurancePrice] AS [InsurancePrice] , [o].[DeliverMobile] AS [DeliverMobile] , 

    [o].[InsuranceType] AS [InsuranceType] , [o].[InsuranceCode] AS [InsuranceCode] , 

    [o].[ReceiverName] AS [ReceiverName]  FROM [JBHP_Orders] [o]

     Left JOIN [JBHP_Payment] [p] ON ( [o].[Id] = [p].[OrderId] )   WHERE ( [o].[Id] = 1962776380424982528 )  AND ( [o].[DeleteMark] = 0 )

    报错关键字 'CASE' 附近有语法错误 ,如果把时间那块换成使用SqlFunc.DateDiff函数是没有问题的

    使用的是最新版 sugar 

    0 回复
  • @fate sta

    0 回复
  • SqlSugarCore 5.1.4.203-preview08


    更新预览版本 08已修复

    0 回复
  • @fate stay night:好的

    0 回复