如何让具有多个表的 QuickGrid 正常工作而不出错?

问题描述 投票:0回答:2

我试图让以下代码工作,但它给了我一个错误:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  CS0411  The type arguments for method 'TypeInference.CreateQuickGrid_0_CaptureParameters<TGridItem>(string, out string, IQueryable<TGridItem>, out IQueryable<TGridItem>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. BlazorApp1  C:\Users\me\source\repos\BlazorApp1\BlazorApp1\Components\Pages\BoardPages\Index.razor  20  
@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
{
    <QuickGrid Class="table" Items="result" >
        <PropertyColumn Property="BoardView => BoardView.Code" />
        <PropertyColumn Property="BoardView => BoardView.Name" />
        <PropertyColumn Property="BoardView => BoardView.LowestPrice" />
        <PropertyColumn Property="BoardView => BoardView.SupplierName" />

        <TemplateColumn Context="board">
            <a href="@($"boards/edit?id={BoardView.ID}")">Edit</a> |
            <a href="@($"boards/details?id={BoardView.ID}")">Details</a> |
            <a href="@($"boards/delete?id={BoardView.ID}")">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; // Move this declaration outside any method or block

    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
                          };

        result = boardPrices.ToList();
    }
    }

我已经尝试了几个小时但没有成功,我只是希望能够从多个表构建自定义数据并将其显示在 QuickGrid 中......

乔恩

期望它能够工作,因为它有正确的类。

c# .net entity-framework blazor
2个回答
1
投票

我通过确保类型正确消除了错误。

private IQueryable<BoardView> boardPrices;

这解决了它。


0
投票

这是我更新的代码,它确实在 DataGrid 中显示数据,但这确实不是一个好方法。我已经尝试了一切让主要的 Iqueryable 工作,但我必须将其发送到列表,然后执行 .ToIquerable...如果有人知道如何排序,请分享。

@page "/boards"
@using Microsoft.AspNetCore.Components.QuickGrid
@inject BlazorApp1.Data.ApplicationDbContext DB
@using BlazorApp1.Data
@rendermode InteractiveServer
@attribute [StreamRendering]


<PageTitle>Index</PageTitle>

<h1>Index</h1>

<p>
    <a href="boards/create">Create New</a>
</p>

<QuickGrid Class="table" Items="@BoardPricesList.AsQueryable()">
        <PropertyColumn Property="@(BoardView => BoardView.Code)" />
        <PropertyColumn Property="@(BoardView => BoardView.Name)" />
        <PropertyColumn Property="@(BoardView => BoardView.LowestPrice)" />
        <PropertyColumn Property="@(BoardView => BoardView.SupplierName)" />

    <TemplateColumn Context="board">
        <a href="@($"boards/edit?id={board.BoardID}")">Edit</a> |
        <a href="@($"boards/details?id={board.BoardID}")">Details</a> |
        <a href="@($"boards/delete?id={board.BoardID}")">Delete</a>
    </TemplateColumn>
    </QuickGrid>


    <ul>
<div>Count: @NumberOfRows</div>
@foreach(var o in BoardPricesList)
{
    
        <li>BoardID: @o.BoardID</li>
        <li>Name: @o.Name</li>
        <li>Code: @o.Code</li>
        <li>Lowest Price: @o.LowestPrice.ToString()</li>
        <li>Supplier Name: @o.SupplierName</li>
}
</ul>

@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 IQueryable<BoardView> boardPrices = Enumerable.Empty<BoardView>().AsQueryable();

    private List<BoardView> BoardPricesList = new List<BoardView>();

    //private IEnumerable<BoardView>? result; // Move this declaration outside any method or block

    public int NumberOfRows = 0;


    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
                          };

       

        //BoardPricesList.ToList<BoardView>();


        foreach (var o in boardPrices)
        {
            BoardPricesList.Add(o);

            NumberOfRows++;
        }
        NumberOfRows = BoardPricesList.Count;

        //result = boardPrices.ToList();
    }
    }
© www.soinside.com 2019 - 2024. All rights reserved.