QueryableExtension.FirstOrDefault make result convert to unexpect dynamic #1628

Closed
opened 2026-01-29 17:56:28 +00:00 by claunia · 2 comments
Owner

Originally created by @holyping on GitHub (Feb 19, 2025).

The code at class QueryableExtension

public static dynamic FirstOrDefault(this IQueryable source)
{
    return source.Provider.Execute(Expression.Call(null,
        typeof(Queryable).GetTypeInfo().GetDeclaredMethods(nameof(Queryable.FirstOrDefault)).FirstOrDefault(mi => mi.IsGenericMethod).MakeGenericMethod(source.ElementType),
        source.Expression));
}

it will capture all the IQueryable<T> objects in project and returns dynamic instead of generic type T.

for example, in RadzenDataGrid<TItem> class, then method OnRowSelect has the code below:

var focusedIndexResult = PagedView
    .Select((x, i) => new { Item = x, Index = i })
    .Where(itemWithIndex => ItemEquals(itemWithIndex.Item, item))
    .FirstOrDefault();

the type of "focusedIndexResult" is dynamic but not anoymous type "new { Item = x, Index = i })"
this is a unexpect code result.

I suggest to change the method name FirstOrDefault of class QueryableExtension to FirstOrDefaultUnTyped.

Originally created by @holyping on GitHub (Feb 19, 2025). The code at class QueryableExtension ``` public static dynamic FirstOrDefault(this IQueryable source) { return source.Provider.Execute(Expression.Call(null, typeof(Queryable).GetTypeInfo().GetDeclaredMethods(nameof(Queryable.FirstOrDefault)).FirstOrDefault(mi => mi.IsGenericMethod).MakeGenericMethod(source.ElementType), source.Expression)); } ``` it will capture all the `IQueryable<T>` objects in project and returns dynamic instead of generic type T. for example, in `RadzenDataGrid<TItem>` class, then method OnRowSelect has the code below: ``` var focusedIndexResult = PagedView .Select((x, i) => new { Item = x, Index = i }) .Where(itemWithIndex => ItemEquals(itemWithIndex.Item, item)) .FirstOrDefault(); ``` the type of "focusedIndexResult" is dynamic but not anoymous type "new { Item = x, Index = i })" this is a unexpect code result. I suggest to change the method name FirstOrDefault of class QueryableExtension to FirstOrDefaultUnTyped.
Author
Owner

@enchev commented on GitHub (Feb 19, 2025):

The extension methods is for non generic IQueryable and will not be listed on generic IQueryable<T>:

Image

Image

@enchev commented on GitHub (Feb 19, 2025): The extension methods is for non generic `IQueryable` and will not be listed on generic `IQueryable<T>`: ![Image](https://github.com/user-attachments/assets/85e19b7c-4070-4ed9-a2ab-57b6ca22e3c9) ![Image](https://github.com/user-attachments/assets/7f4ae50d-444c-49b8-90a4-53d53720eaad)
Author
Owner

@holyping commented on GitHub (Feb 20, 2025):

@enchev Thank you for your reply.
I found a code at RadzenDataGrid.razor.cs, line 2703 (method OnRowSelect) the FirstOrDefault returns dynamic but it is from IQueryable<TSource>
And also the codes below:
RadzenDataGrid.razor.cs line 1727 in method GetSelfRefView(IQueryable<TItem> view, string orderBy)
RadzenDataGrid.razor.cs line 1813 in property View

Will you check it? thanks!

Image

@holyping commented on GitHub (Feb 20, 2025): @enchev Thank you for your reply. I found a code at RadzenDataGrid.razor.cs, line 2703 (method OnRowSelect) the FirstOrDefault returns dynamic but it is from `IQueryable<TSource>` And also the codes below: RadzenDataGrid.razor.cs line 1727 in method `GetSelfRefView(IQueryable<TItem> view, string orderBy)` RadzenDataGrid.razor.cs line 1813 in property View Will you check it? thanks! ![Image](https://github.com/user-attachments/assets/280eda38-3bd9-4422-801d-37b64935ff2e)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#1628