Hi everyone,
I'm having trouble understanding why I can't use an immutable array in the Data property of RadzenDataGrid. Since it implements IEnumerable, I expected it to work. Could someone please explain why this is not possible ?
Thank you!
Hi everyone,
I'm having trouble understanding why I can't use an immutable array in the Data property of RadzenDataGrid. Since it implements IEnumerable, I expected it to work. Could someone please explain why this is not possible ?
Thank you!
Hi everyone,
I'm detailing my previous post.I've looked into the code, but I'm having trouble understanding the issue because the parameter is an IEnumerable
, and ImmutableArray
implements IEnumerable
.
Example Code:
<RadzenDataGrid AllowVirtualization="true" PageSize="2" Data="@Items" EmptyText="Empty"
AllowPaging="true" TItem="CustomClass" AllowFiltering="true" AllowSorting="true">
<Columns>
<RadzenDataGridColumn TItem="CustomClass" Property="@nameof(CustomClass.Name)" />
<RadzenDataGridColumn TItem="CustomClass" Property="@nameof(CustomClass.Description)" />
</Columns>
</RadzenDataGrid>
@code {
public class CustomClass
{
public CustomClass(string name, string description)
{
Name = name;
Description = description;
}
public string Name { get; set; }
public string Description { get; set; }
}
private ImmutableArray<CustomClass> Items = ImmutableArray.Create(
new CustomClass("test1", "description test1"),
new CustomClass("test2", "description test2"),
new CustomClass("test3", "description test3"),
new CustomClass("test4", "description test4")
);
}
Exception thrown during render:
blazor.web.js:1 [2025-04-24T12:18:54.608Z] Error: System.ArgumentException: Expression of type 'System.Collections.Immutable.ImmutableArray`1[CustomClass]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[CustomClass]' of method 'Int32 Count[CustomClass](System.Collections.Generic.IEnumerable`1[CustomClass])' (Parameter 'arg0')
at Microsoft.AspNetCore.Components.Web.Virtualization.Virtualize`1.BuildRenderTree(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
I spoke too soon. The exception is raised when you attempt to convert this collection to IQueryable and execute Count(). The following code will raise the same exception:
@using System.Collections.Immutable
@code {
public class CustomClass
{
public CustomClass(string name, string description)
{
Name = name;
Description = description;
}
public string Name { get; set; }
public string Description { get; set; }
}
private ImmutableArray<CustomClass> Items = ImmutableArray.Create(
new CustomClass("test1", "description test1"),
new CustomClass("test2", "description test2"),
new CustomClass("test3", "description test3"),
new CustomClass("test4", "description test4")
);
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
var count = Items.AsQueryable().Count();
}
}
This is what the DataGrid is performing with every collection assigned to Data in order to calculate the paging.
Hi,
Thank you for your response.
It is the ImmutableArray
class, not a generic class.
The ImmutableArray
class implements the IEnumerable
interface.
Yes, I saw that - see my previous reply.
Okay, I understand more about the issue, but I have two questions:
Data
parameter need to implement IQueryable
? Is it not dangerous to perform a cast in the component?Count
method from IEnumerable
?AsQueryable()
is not a cast.
No, since in this case we need to have separate code with different implementation.
Oh, I see. I apologize for not reading your message carefully.
After reviewing the documentation for AsQueryable, I still don't understand why it isn't working.