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

Razor Page司机排班报表开发及数据更新异常求助

问题:Razor Page司机排班报表修改Trip表数据后无返回值

现有数据库Trip表存储完整行程数据,需开发Razor Page司机排班报表,将接送行程拆分为「出发-到达」条目并按时间排序。尝试通过IQueryable分别创建接送段、返程段两个查询,用Concat拼接后生成报表,但修改Trip表数据后报表无返回值。

相关代码示例

Index.cs

public class IndexModel : PageModel
{
    private readonly AppDbContext _context;

    public IndexModel(AppDbContext context)
    {
        _context = context;
    }

    public IQueryable<ScheduleItem> ScheduleItems { get; set; }

    public void OnGet()
    {
        // 接送段查询
        var pickupSegments = _context.Trips
            .Where(t => t.Status == TripStatus.Active)
            .Select(t => new ScheduleItem
            {
                DriverId = t.DriverId,
                Time = t.PickupTime,
                Type = "接送",
                Location = t.PickupLocation,
                Destination = t.DropoffLocation
            });

        // 返程段查询
        var returnSegments = _context.Trips
            .Where(t => t.Status == TripStatus.Active && t.ReturnTime.HasValue)
            .Select(t => new ScheduleItem
            {
                DriverId = t.DriverId,
                Time = t.ReturnTime.Value,
                Type = "返程",
                Location = t.DropoffLocation,
                Destination = t.PickupLocation
            });

        // 拼接并排序
        ScheduleItems = pickupSegments.Concat(returnSegments)
            .OrderBy(s => s.Time);
    }
}

public class ScheduleItem
{
    public int DriverId { get; set; }
    public DateTime Time { get; set; }
    public string Type { get; set; }
    public string Location { get; set; }
    public string Destination { get; set; }
}

Index.cshtml

@page
@model IndexModel

<h1>司机排班报表</h1>

<table class="table">
    <thead>
        <tr>
            <th>司机ID</th>
            <th>时间</th>
            <th>类型</th>
            <th>出发地</th>
            <th>目的地</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.ScheduleItems)
        {
            <tr>
                <td>@item.DriverId</td>
                <td>@item.Time.ToString("yyyy-MM-dd HH:mm")</td>
                <td>@item.Type</td>
                <td>@item.Location</td>
                <td>@item.Destination</td>
            </tr>
        }
    </tbody>
</table>

解决方法

  • 强制立即执行查询,规避延迟执行问题:IQueryable是延迟执行的,修改数据后若复用旧查询实例,可能不会拉取最新数据。将拼接后的查询转为List,确保每次请求都获取实时数据:

    // 修改属性类型为List<ScheduleItem>
    public List<ScheduleItem> ScheduleItems { get; set; }
    
    // 赋值时立即执行查询
    ScheduleItems = pickupSegments.Concat(returnSegments)
        .OrderBy(s => s.Time)
        .ToList();
    
  • 校验两个查询的实体结构一致性:Concat要求两个IQueryable返回的实体类型、属性(包括数据类型、可空性)完全匹配。比如若returnSegments的Time是可空类型,而pickupSegments的Time不可空,会导致拼接失败,需确保ScheduleItem的属性投影完全一致。

  • 验证拼接后的SQL语句:在EF Core中开启数据库命令日志,查看拼接后的SQL,直接在数据库中执行确认是否有返回结果。可在appsettings.json中添加配置:

    "Logging": {
      "LogLevel": {
        "Microsoft.EntityFrameworkCore.Database.Command": "Information"
      }
    }
    

    通过日志输出的SQL排查是否修改数据后过滤条件导致无数据。

  • 检查数据过滤逻辑:确认修改后的Trip数据是否符合pickupSegments和returnSegments的过滤规则,比如行程Status是否仍为Active、ReturnTime是否非空,避免因数据状态变化导致两个查询都无结果。

  • 确认上下文生命周期:确保AppDbContext采用Scoped生命周期(默认配置),避免因上下文复用读取缓存数据,每次请求都使用新的上下文实例。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 10:41:17