You need to enable JavaScript to run this app. 使用LINQ关联两张表按航班号计算机票总售价的实现方法
优惠活动
大模型
产品
解决方案
定价
更多

使用LINQ关联两张表按航班号计算机票总售价的实现方法

航班机票总售卖金额LINQ实现方案

业务说明

核心需求为根据指定航班号(FlightNo)关联BookingSet航班预订表与PassengerSet乘客表,对匹配到的所有机票价格(TicketPrice)做求和计算,例如航班号为1时,汇总金额应为19500。

代码迭代与问题修正

1. 第一版缺陷代码

[HttpGet("{id}/sales")]
public async Task<ActionResult<double>> GetFlightTotalSales(int id)
{
    double price = 0.0;
    var bookingset = _context.BookingSet;
    var passengerset = await _context.PassengerSet.ToListAsync();

    var passengerIdFlight = bookingset
                        .Where(x => x.FlightNo == id)
                        .Select(x => x.PassengerID);
                
    price = passengerset.Where(x => ( x.PersonID == passengerIdFlight).Select(x=>x.TicketPrice);

    return price;
}

存在的问题:

  • 单个PersonID无法直接和passengerIdFlight(PassengerID集合)做等值判断,集合匹配应使用Contains方法
  • 语法错误,Where子句后括号未闭合,无法通过编译
  • 提前加载全量乘客表数据到内存,造成不必要的资源占用,EF Core可直接在数据库端完成关联查询

2. 第二版代码(仍未达预期)

[HttpGet("{id}/sales")]
public ActionResult<double> GetFlightTotalSales(int id)
{
    var price2 = 0.0;
    var results = (from b in _context.BookingSet
                    join p in _context.PassengerSet on b.PassengerID equals p.PersonID
                    select new { b = b, p = p }
                    ).GroupBy(x => new { passengerID = x.b.PassengerID, flight = id })
                    .Select(x => new
                    {
                        flight = x.Key.flight,
                        firstName = x.First().p.Firstname,
                        lastName = x.First().p.Lastname,
                        totalPrice =(double)x.Sum(y => y.p.TicketPrice)
                    });
    

    foreach(var price in results)
    {
        price2 = price2 + price.totalPrice;
        
    }

    return price2;
}

存在的问题:

  • 分组key中硬编码外部传入的flight = id,分组逻辑完全脱离实际的航班字段,仅能适配单航班查询场景,扩展性极差
  • 冗余查询了非必要的乘客姓名字段,造成不必要的性能开销

3. 最终可运行实现

[HttpGet("{id}/TotalSalePrice")]
public ActionResult<double> GetFlightTotalSales(int id)
{
    var price2 = 0.0;
    var results = (from b in _context.BookingSet
                    join p in _context.PassengerSet on b.PassengerID equals p.PersonID
                    select new { b = b, p = p }
                    ).GroupBy(x => new { passengerID = x.b.PassengerID, flight = x.b.FlightNo })
                    .Where(z=>z.Key.flight == id)
                    .Select(x =>(double)x.Sum(y => y.p.TicketPrice)
                    );
    

    foreach(var price in results)
    {
        price2 = price2 + price;   
    }

    return price2;
}

可选性能优化:上述实现中的内存遍历累加、冗余分组逻辑可直接简化,EF Core会自动将查询转换为SQL在数据库端执行,大幅减少数据传输和内存占用:

[HttpGet("{id}/TotalSalePrice")]
public async Task<ActionResult<double>> GetFlightTotalSales(int id)
{
    // 直接过滤目标航班、关联乘客表后对机票价格求和
    var totalSales = await (from b in _context.BookingSet
                        join p in _context.PassengerSet on b.PassengerID equals p.PersonID
                        where b.FlightNo == id
                        select p.TicketPrice)
                        .SumAsync();
    return totalSales;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.11 09:15:01