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

Blazor Server应用使用Blazor.IndexedDB按日期查询IndexedDB无结果问题

问题原因及解决方案

1. 精确匹配逻辑不符合存储数据特征

你当前使用的GetAllRecordsByIndex默认执行精确等值匹配,传入的查询值为当日T00:00:00,但存储的date字段是数据录入时的完整ISO时间字符串(包含时分秒),二者无法等值匹配,导致返回结果为空。
需要改为范围查询,匹配大于等于当日零点、小于次日零点的所有数据。

2. 异步方法签名错误导致UI不刷新

你使用了async void定义查询方法,Blazor中async void方法执行完成后不会触发组件的状态重渲染,即便查询到数据也不会在页面展示,需要改为async Task。

3. 实体类字段大小写需与索引配置匹配

你配置的索引KeyPath为小写date,请确认你的Words实体类中对应的时间属性命名为小写date,如果是大写Date,序列化后存入IndexedDB的字段名会和索引配置不匹配,导致查询失效。


修改后的代码示例
protected List<Words> todaysWords { get; set; } = new List<Words>();
protected string selectedIndex { get; set; } = "date";
// 定义查询范围:当日零点 到 次日零点
protected string startOfDay { get; set; } = DateTime.Today.ToString("yyyy-MM-dd") + "T00:00:00";
protected string endOfDay { get; set; } = DateTime.Today.AddDays(1).ToString("yyyy-MM-dd") + "T00:00:00";

protected override async Task OnInitializedAsync()
{
    await SearchForTodaysWords();
}

protected async Task SearchForTodaysWords()
{
    if (string.IsNullOrEmpty(selectedIndex))
    {
        return;
    }

    var indexSearch = new StoreIndexQuery<string>
    {
        Storename = DbManager.Stores[0].Name,
        IndexName = selectedIndex,
        LowerBound = startOfDay,
        UpperBound = endOfDay,
        // 包含下界,不包含上界,避免匹配到次日零点的临界数据
        LowerBoundOpen = false,
        UpperBoundOpen = true
    };

    var db = await DbManager.GetAllRecordsByIndex<string, Words>(indexSearch);
    
    if (db is null)
    {
        return;
    }

    todaysWords.Clear();
    todaysWords.AddRange(db);
}

注意:如果之前修改过索引配置或者实体类字段名,请先清除浏览器中已存储的旧IndexedDB数据,旧版本的存储结构不会自动更新,会导致配置不生效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 03:09:00