关于SqlFunc.AggregateDistinctCount的使用 返回

SqlSugar 沟通中
4 125
该叫什么 心然 发布于2周前
悬赏:0 飞吻

想要实现的Oracle SQL:select count(distinct order_no) from order_detail where ...

以下为实现的通用函数定义:

public int GetDistinctCount<TField>(Expression<Func<T, TField>> selector, Expression<Func<T, bool>> predicate = null)

{

    var query = _db.Queryable<T>().WhereIF(predicate != null, predicate); ;


    // 使用 SqlFunc.AggregateDistinctCount 进行去重计数

    return query.Select(x => SqlFunc.AggregateDistinctCount(selector)).Single();

}

调用代码例子:

int count=GetDistinctCount(x=>x.order_no,x=>x.order_no.Contains('202601'));

解析的结果:

select count(distinct :order_no01) from order_detail where order_no like '%202601%'

:order_no01 值为:x=>x.order_no

返回的结果都是:1,与实际的查询结果不一致。

不知以上函数定义哪里不对,请指教。

热忱回答4

  •  以SQL为准。你要想的SQL是什么样。

    0 回复
  • AOP监控SQL比较SQL和你想要的差异

    0 回复
  • int totalCount = await WoProcessViewService.GetDistinctCountAsync(x=>x.WORKNO,whereClause);

    AOP获取:

    DbContext: Information: 

    [Sql]:SELECT COUNT(DISTINCT :MethodConst0 ) FROM "WO_PROCESS"  WHERE PROCESS_STATUS<>'已完工'  

    [Pars]:

    [Name]::MethodConst0 [Value]:x => x.WORKNO [Type]:AnsiString

    结果为:1

    实际查询SQL: SELECT COUNT(DISTINCT WORKNO ) FROM "WO_PROCESS"  WHERE PROCESS_STATUS<>'已完工'  

    正确查询结果为:10

    所以,不清楚这个函数定义哪里不正确:GetDistinctCount

    0 回复
  • 函数定义:

    public async Task<int> GetDistinctCountAsync<TField>(Expression<Func<T, TField>> selector, string whereSql = null)

    {

        var query = _db.Queryable<T>().WhereIF(!string.IsNullOrWhiteSpace(whereSql), whereSql);


        // 使用 SqlFunc.AggregateDistinctCount 进行去重计数

        return await query.Select(x => SqlFunc.AggregateDistinctCount(selector)).SingleAsync();

    }


    0 回复