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

RadzenDataGrid中Timespan类型字段过滤失效问题求助

RadzenDataGrid TimeSpan字段过滤无效问题解决

问题描述

我在RadzenDataGrid中展示数据库的TimeSpan类型数据,对该字段执行过滤(比如“不为空”)时完全没效果,且args.Filter仅显示it => ,没有有效值。想确认RadzenDataGrid是否支持TimeSpan字段过滤,以下是我的实现代码:

原Grid代码

<RadzenDataGrid @ref="_grid" AllowFiltering="true" AllowColumnResize="true" 
                 FilterMode="@FilterMode.Advanced" LoadData="@LoadData" Data="@_shipments">
  <Columns>
    <RadzenDataGridColumn TItem="SdShipment" Property="@nameof(SdShipment.OrderNumber)" Title="Order Number" Frozen="true" />
    <RadzenDataGridColumn TItem="Shipment" Property="LifeSpan" Title="Life Span" Sortable="true" Filterable="true">
        <Template Context="data">
            @((data.LifeSpan.HasValue) ? data.LifeSpan.Value.Days.ToString() : "N/A")
        </Template>
    </RadzenDataGridColumn>
    <RadzenDataGridColumn TItem="Shipment" Property="Material.Type" Title="Material Type" />
    <RadzenDataGridColumn TItem="Shipment" Property="Asset.AssetType" Title="Asset Type" />
  </Columns>
</RadzenDataGrid>

原LoadData方法

private async Task LoadData(LoadDataArgs args)
{
  _isLoading = true;
  await InvokeAsync(StateHasChanged);
       
  try
  {
    await using var context = await ContextFactory.CreateDbContextAsync();
    var query = context.shipments
                .AsQueryable();

    var filterActive = false;
    if(!string.IsNullOrEmpty(args.Filter))
    {
      query = query.Where(args.Filter);
      filterActive = true;
    }
    
    if(!string.IsNullOrEmpty(args.OrderBy))
    {                
      query = query.OrderBy(args.OrderBy);
    }
      _shipments = query.Skip(args.Skip ?? 0).Take(args.Top ?? 250).ToList();
    }
    catch(Exception ex)
    {
      // 异常处理逻辑
    }
    finally
    {
      _isLoading = false;
      await InvokeAsync(StateHasChanged);
    }
}

解决方案

RadzenDataGrid默认支持TimeSpan字段过滤,问题出在代码实现的两个关键点上:

1. 统一列的TItem类型

Grid根节点的TItem必须和所有列的TItem保持一致,你第一列用的是SdShipment,但LifeSpan等列用的是Shipment,这会导致Radzen无法正确识别字段类型,生成的过滤表达式不完整。修改所有列的TItem为SdShipment即可。

2. 可选:自定义FilterTemplate适配可空TimeSpan

对于可空TimeSpan类型,默认过滤组件可能适配不佳,自定义FilterTemplate可以明确过滤逻辑,确保生成有效表达式。

修改后的完整代码

<RadzenDataGrid @ref="_grid" AllowFiltering="true" AllowColumnResize="true" 
                 FilterMode="@FilterMode.Advanced" LoadData="@LoadData" Data="@_shipments" TItem="SdShipment">
  <Columns>
    <RadzenDataGridColumn TItem="SdShipment" Property="@nameof(SdShipment.OrderNumber)" Title="Order Number" Frozen="true" />
    <RadzenDataGridColumn TItem="SdShipment" Property="LifeSpan" Title="Life Span" Sortable="true" Filterable="true">
        <Template Context="data">
            @((data.LifeSpan.HasValue) ? data.LifeSpan.Value.Days.ToString() : "N/A")
        </Template>
        <!-- 自定义FilterTemplate,适配可空TimeSpan -->
        <FilterTemplate>
            <RadzenFilterOperatorSelector @bind-Value=@context.Operator />
            <RadzenTimeSpanPicker @bind-Value=@context.Value ShowSeconds="false" />
        </FilterTemplate>
    </RadzenDataGridColumn>
    <RadzenDataGridColumn TItem="SdShipment" Property="Material.Type" Title="Material Type" />
    <RadzenDataGridColumn TItem="SdShipment" Property="Asset.AssetType" Title="Asset Type" />
  </Columns>
</RadzenDataGrid>

额外检查点

  • 确认SdShipment类中的LifeSpan属性是TimeSpan?(可空TimeSpan)类型;
  • 确保EF Core能将生成的过滤表达式正确转换为SQL查询,避免客户端过滤导致的性能问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 14:25:18