You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LINQ查询中对关联表的amount字段求和并返回至DataGrid

解决子表金额求和并返回至DataGrid的问题

我明白你现在的需求:要对关联的billing_transaction_accessorial_charge表中amount字段求和,同时保留原有主表的所有查询字段,最终返回给DataGrid。既然已经配置好了表之间的关联关系,直接利用EF的导航属性就能轻松实现这个需求,不用复杂的手动Join操作。

核心修改点

你当前的查询里已经用到了d.billing_transaction_accessorial_charge.Count来统计子表记录数,只需要把这个统计逻辑换成求和操作即可。需要注意的是,如果某些主表记录没有对应的子表附加费用,直接用Sum会返回null,所以我们要处理这种空值情况,默认返回0。

修改后的完整查询代码

var gridData = (from d in db.billing_transactions 
                where d.status == 1 
                select new { 
                    d.base_amount, 
                    d.Id, 
                    drivers_name = d.stop_details.driver_details.first_name + " " + d.stop_details.driver_details.last_name, 
                    d.billing_note, 
                    d.correcting_entry, 
                    d.fuel_surcharge, 
                    d.invoice_number, 
                    d.net_amount, 
                    d.original_billing_transaction_id, 
                    d.pcs_billed, 
                    billing_transactions_status = d.status, 
                    billing_transaction_status_desc = d.billing_lookup_transaction_status_names.status, 
                    d.customer_id, 
                    d.rule_id, 
                    d.weight_billed, 
                    d.stop_details.con_name, 
                    d.stop_details.con_address1, 
                    d.stop_details.con_city, 
                    d.stop_details.con_state, 
                    d.stop_details.con_zip, 
                    d.stop_details.assigned_driver_id, 
                    d.stop_details.billing_base_rate, 
                    d.stop_details.billing_geoZone_id, 
                    d.stop_details.billing_weight_class_id, 
                    d.stop_details.cust_ref_1_BOL, 
                    d.stop_details.cust_ref_2_OrderNum, 
                    d.stop_details.cust_ref_3_stopID, 
                    d.stop_details.cust_ref_4_routeID, 
                    d.stop_details.cust_ref_5_terminalID, 
                    d.stop_details.items_del_ytd, 
                    d.stop_details.lading_qty_haz, 
                    d.stop_details.lading_qty_total, 
                    d.stop_details.lading_wgt_haz, 
                    d.stop_details.lading_wgt_total, 
                    d.stop_details.note_from_cust, 
                    d.stop_details.pallets_qty_total, 
                    d.stop_details.pallets_wgt_total, 
                    d.stop_details.ship_date, 
                    stop_status = d.stop_details.status, 
                    d.stop_details.stop_canceled, 
                    d.stop_details.stop_changed, 
                    d.stop_details.verified_ship_date, 
                    d.stop_details.verified_timestamp, 
                    d.stop_details.wgt_del_ytd, 
                    d.customer.customer_name, 
                    // 替换原count为子表金额总和,处理空值情况
                    accessorial_total = d.billing_transaction_accessorial_charge.Sum(ac => (decimal?)ac.amount) ?? 0,
                    // 如果还需要保留记录数,可以继续保留这个字段
                    accessorial_count = d.billing_transaction_accessorial_charge.Count 
                } ).ToArray();

关键说明

  1. 求和逻辑:d.billing_transaction_accessorial_charge.Sum(ac => (decimal?)ac.amount) 这里把amount转成可空的decimal类型,是为了避免当子表没有记录时,Sum方法返回null导致的异常。
  2. 空值处理:?? 0 表示如果求和结果是null(即没有附加费用),就默认返回0,保证DataGrid里的数值不会显示为空或报错。
  3. 导航属性的优势:因为你已经配置了主表和子表的关联关系,EF会自动帮你处理关联查询,不需要手动写Join语句,代码更简洁易读。

之后你只需要在DataGrid中新增一列绑定accessorial_total字段,就能展示每条主交易对应的附加费用总和了。

内容的提问来源于stack exchange,提问作者Joe Ruder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:45:19