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

如何让基于多表数据源的QuickGrid正常运行无报错?

QuickGrid CS0411错误修复及多表数据展示方案

问题根源

出现CS0411类型推断错误,同时代码存在三处写法问题:

  • QuickGrid无法自动推断泛型参数TGridItem的类型,需显式指定
  • PropertyColumn的属性选择器错误使用字符串包裹lambda表达式,导致组件无法识别属性映射
  • TemplateColumn中错误通过类名访问属性,未使用定义的上下文变量,且属性名称不匹配

修正后的完整代码

@page "/boards"
@using Microsoft.AspNetCore.Components.QuickGrid
@inject BlazorApp1.Data.ApplicationDbContext DB
@using BlazorApp1.Data

<PageTitle>Index</PageTitle>

<h1>Index</h1>

<p>
    <a href="boards/create">Create New</a>
</p>
@if (result == null)
{
    <div>Loading....</div>
}else
{
    <!-- 显式指定TGridItem类型为BoardView,解决类型推断问题 -->
    <QuickGrid<BoardView> Class="table" Items="result">
        <!-- 修正PropertyColumn写法:直接传入lambda表达式,无需引号 -->
        <PropertyColumn Property="@(item => item.Code)" />
        <PropertyColumn Property="@(item => item.Name)" />
        <PropertyColumn Property="@(item => item.LowestPrice)" />
        <PropertyColumn Property="@(item => item.SupplierName)" />

        <!-- 使用上下文变量item,引用正确的BoardID属性 -->
        <TemplateColumn Context="item">
            <a href="@($"boards/edit?id={item.BoardID}")">Edit</a> |
            <a href="@($"boards/details?id={item.BoardID}")">Details</a> |
            <a href="@($"boards/delete?id={item.BoardID}")">Delete</a>
        </TemplateColumn>
    </QuickGrid>
}

@code {
    public class BoardView
    {
        public int BoardID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public double LowestPrice { get; set; }
        public string SupplierName { get; set; }
    }
    
    private IEnumerable<BoardView>? result;

    protected override async Task OnInitializedAsync()
    {
        var boardPrices = from board in DB.Board
                          join boardSupplier in DB.BoardSupplier on board.ID equals boardSupplier.Board.ID
                          join supplier in DB.Supplier on boardSupplier.Supplier.Id equals supplier.Id
                          group new { board, boardSupplier, supplier } by new { board.ID, board.Code, board.Name } into grouped
                          select new BoardView
                          {
                              BoardID = grouped.Key.ID,
                              Code = grouped.Key.Code,
                              Name = grouped.Key.Name,
                              LowestPrice = grouped.Min(x => x.boardSupplier.Price),
                              SupplierName = grouped.FirstOrDefault(x => x.boardSupplier.Price == grouped.Min(y => y.boardSupplier.Price)).supplier.Name
                          };

        // 使用异步ToListAsync替代同步ToList,适配EF Core异步操作
        result = await boardPrices.ToListAsync();
    }
}

核心修改说明

  1. 显式指定QuickGrid泛型类型:通过<QuickGrid<BoardView>>明确实体类型,直接解决CS0411类型推断错误
  2. 修复PropertyColumn属性选择器:去掉字符串包裹,使用@(item => item.PropertyName)的lambda表达式形式,让组件正确识别绑定属性
  3. 修正TemplateColumn上下文引用:使用定义的Context变量访问实体属性,同时注意BoardView的ID属性是BoardID而非ID
  4. 异步优化:将ToList()替换为await ToListAsync(),符合EF Core异步查询最佳实践,避免阻塞UI线程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 16:11:19