Forum Discussion
JonathanBuckle
Feb 22, 2024Copper Contributor
DataGrid Custom Queries Linq Not working as expected
Hi,
I am new to Blazor and have an issue with DataGrid and custom EF LINQ, I can display data from a model without an issue, however had to really go about the houses to get the DataGrid to display a collection of data from multiple tables. The LINQ works and the code now works but there must be a better way.
I have tried using boardPrices directly... but that doesn't show any data in the DataGrid, instead I have to foreach it into a list then .ToQueryable... doesn't seam right to me.
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();
}
}
No RepliesBe the first to reply