明细表查询、明细表合计

自定义明细表查询

注意:明细表查询是写在[主表service.cs]中生成的主从表编辑弹出框中明细表数据查询

     /// <summary>
    /// 明细表查询
    /// </summary>
    /// <param name="pageData"></param>
    /// <returns></returns>
    public override object GetDetailPage(PageDataOptions pageData)
    {
        //自定义查询胆细表

        ////明细表自定义查询方式一:EF
        //var query = SellOrderListRepository.Instance.IQueryablePage<SellOrderList>(
        //     pageData.Page,
        //     pageData.Rows,
        //     out int count,
        //     x => x.Order_Id == pageData.Value.GetGuid(),
        //      orderBy: x => new Dictionary<object, QueryOrderBy>() { { x.CreateDate, QueryOrderBy.Desc } }
        //    );
        //PageGridData<SellOrderList> detailGrid = new PageGridData<SellOrderList>();
        //detailGrid.rows = query.ToList();
        //detailGrid.total = count;

        ////明细表自定义查询方式二:dapper
        //string sql = "select count(1) from SellOrderList where Order_Id=@orderId";
        //detailGrid.total = repository.DapperContext.ExecuteScalar(sql, new { orderId = pageData.Value }).GetInt();

        //sql = @$"select * from (
        //              select *,ROW_NUMBER()over(order by createdate desc) as rowId
        //           from SellOrderList where Order_Id=@orderId
        //        ) as s where s.rowId between {((pageData.Page - 1) * pageData.Rows + 1)} and {pageData.Page * pageData.Rows} ";
        //detailGrid.rows = repository.DapperContext.QueryList<SellOrderList>(sql, new { orderId = pageData.Value });

        //return detailGrid;

        return base.GetDetailPage(pageData);
    }

明细表合计(见上图)

注意:明细表查询是写在[主表service.cs]中生成的主从表编辑弹出框中明细表数据查询

    protected override? object GetDetailSummary<Detail>(IQueryable<Detail> queryeable)
    { 
        //需要与【前端开发】文档上的合计一起使用
        //判断是哪张明细表
        if (typeof(Detail)==typeof(SellOrderList))
        {
            //ef写法(需要与前端开发文档上的【table显示合计】一起使用)
            return ((IQueryable<SellOrderList>)queryeable).GroupBy(x => 1).Select(x => new
            {
                //Weight/Qty注意大小写和数据库字段大小写一样
                Weight = x.Sum(o => o.Weight),
                Qty = x.Sum(o => o.Qty)
            }).FirstOrDefault();

           //sqlsugar写法(需要与前端开发文档上的【table显示合计】一起使用)
           return (ISugarQueryable<SellOrder> queryable).Select(x => new 
                 {
                    FundsSum = SqlFunc.AggregateSum(x.FundsSum),
                    HasPayedSum = SqlFunc.AggregateSum(x.HasPayedSum),
                    NeedPaySum = SqlFunc.AggregateSum(x.NeedPaySum),
                    Items = SqlFunc.AggregateSum(x.Items)
                }).FirstOrDefault();
        }
        return null
    }
Last Updated 2025/4/15 14:51:28