Skip to content

Commit

Permalink
Provide parsing config when using Dynamic Linq methods in order to im…
Browse files Browse the repository at this point in the history
…prove initial performance.
  • Loading branch information
akorchev committed May 20, 2024
1 parent 47e9b4b commit bd1a5ef
Show file tree
Hide file tree
Showing 24 changed files with 74 additions and 76 deletions.
4 changes: 1 addition & 3 deletions Radzen.Blazor/CartesianSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Linq.Dynamic.Core;
using Radzen.Blazor.Rendering;
using System.Threading.Tasks;
using System.Collections;
using Microsoft.AspNetCore.Components.Rendering;

namespace Radzen.Blazor
{
Expand Down Expand Up @@ -410,7 +408,7 @@ public override async Task SetParametersAsync(ParameterView parameters)

if (IsDate(CategoryProperty) || IsNumeric(CategoryProperty))
{
Items = Items.AsQueryable().OrderBy(CategoryProperty).ToList();
Items = Items.AsQueryable().OrderBy(DynamicLinqCustomTypeProvider.ParsingConfig, CategoryProperty).ToList();
}
}

Expand Down
12 changes: 6 additions & 6 deletions Radzen.Blazor/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,12 @@ public class SchedulerAppointmentMoveEventArgs
{
/// <summary>
/// Gets or sets the appointment data.
/// </summary>
/// </summary>
public AppointmentData Appointment { get; set; }

/// <summary>
/// Gets or sets the time span.
/// </summary>
/// </summary>
public TimeSpan TimeSpan { get; set; }
}

Expand Down Expand Up @@ -938,7 +938,7 @@ public FileInfo(IBrowserFile source)
/// <summary>
/// Gets the name of the selected file.
/// </summary>
public string Name
public string Name
{
get
{
Expand Down Expand Up @@ -2719,7 +2719,7 @@ public class TreeItemSettings
public class TreeItemRenderEventArgs
{
/// <summary>
/// Gets or sets the item HTML attributes.
/// Gets or sets the item HTML attributes.
/// </summary>
public IDictionary<string, object> Attributes { get; private set; } = new Dictionary<string, object>();

Expand All @@ -2734,7 +2734,7 @@ internal bool CheckedSet()
/// Gets or sets a value indicating whether this item is checked.
/// </summary>
/// <value><c>true</c> if expanded; otherwise, <c>false</c>.</value>
public bool? Checked
public bool? Checked
{
get
{
Expand Down
2 changes: 1 addition & 1 deletion Radzen.Blazor/DataBoundFormComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ protected virtual IEnumerable View

query.Add($"{Enum.GetName(typeof(StringFilterOperator), FilterOperator)}(@0)");

_view = Query.Where(String.Join(".", query), ignoreCase ? searchText.ToLower() : searchText);
_view = Query.Where(DynamicLinqCustomTypeProvider.ParsingConfig, string.Join(".", query), ignoreCase ? searchText.ToLower() : searchText);
}
else
{
Expand Down
20 changes: 10 additions & 10 deletions Radzen.Blazor/DropDownBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,11 @@ protected virtual async System.Threading.Tasks.Task HandleKeyPress(Microsoft.Asp

Debounce(DebounceFilter, FilterDelay);
}
else
else
{
var filteredItems = GetView(items.AsQueryable(),
args.Key,
StringFilterOperator.StartsWith,
var filteredItems = GetView(items.AsQueryable(),
args.Key,
StringFilterOperator.StartsWith,
FilterCaseSensitivity.CaseInsensitive)
.Cast<object>()
.ToList();
Expand Down Expand Up @@ -775,7 +775,7 @@ protected virtual async System.Threading.Tasks.Task HandleKeyPress(Microsoft.Asp
selectedIndex = result.Index;
}
await JSRuntime.InvokeVoidAsync("Radzen.selectListItem", list, list, result.Index);
}
}
}

preventKeydown = false;
Expand Down Expand Up @@ -1108,7 +1108,7 @@ IQueryable GetView(IQueryable source, string value, StringFilterOperator? op = n
}
else
{
result = source.Where(String.Join(".", query), search);
result = source.Where(DynamicLinqCustomTypeProvider.ParsingConfig, string.Join(".", query), search);
}
}
else
Expand Down Expand Up @@ -1297,7 +1297,7 @@ internal void UpdateSelectedItems(object item)
}
else
{
selectedItems = selectedItems.AsQueryable().Where($@"!object.Equals(it.{ValueProperty},@0)", value).ToList();
selectedItems = selectedItems.AsQueryable().Where(DynamicLinqCustomTypeProvider.ParsingConfig, $@"!object.Equals(it.{ValueProperty},@0)", value).ToList();
}
}
else
Expand Down Expand Up @@ -1332,7 +1332,7 @@ protected virtual void SelectItemFromValue(object value)
}
else
{
SelectedItem = view.AsQueryable().Where($@"{ValueProperty} == @0", value).FirstOrDefault();
SelectedItem = view.AsQueryable().Where(DynamicLinqCustomTypeProvider.ParsingConfig, $@"{ValueProperty} == @0", value).FirstOrDefault();
}
}
else
Expand All @@ -1359,10 +1359,10 @@ protected virtual void SelectItemFromValue(object value)
}
else
{
item = view.AsQueryable().Where($@"{ValueProperty} == @0", v).FirstOrDefault();
item = view.AsQueryable().Where(DynamicLinqCustomTypeProvider.ParsingConfig, $@"{ValueProperty} == @0", v).FirstOrDefault();
}

if (!object.Equals(item, null) && !selectedItems.AsQueryable().Where($@"object.Equals(it.{ValueProperty},@0)", v).Any())
if (!object.Equals(item, null) && !selectedItems.AsQueryable().Where(DynamicLinqCustomTypeProvider.ParsingConfig, $@"object.Equals(it.{ValueProperty},@0)", v).Any())
{
selectedItems.Add(item);
}
Expand Down
18 changes: 18 additions & 0 deletions Radzen.Blazor/DynamicLinqCustomTypeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Reflection;

namespace Radzen.Blazor
{
class DynamicLinqCustomTypeProvider : IDynamicLinkCustomTypeProvider
{
static readonly HashSet<Type> empty = [];
public HashSet<Type> GetCustomTypes() => empty;
public Dictionary<Type, List<MethodInfo>> GetExtensionMethods() => throw new NotSupportedException();
public Type ResolveType(string typeName) => throw new NotSupportedException();
public Type ResolveTypeBySimpleName(string simpleTypeName) => throw new NotSupportedException();
public static ParsingConfig ParsingConfig = new() { CustomTypeProvider = new DynamicLinqCustomTypeProvider() };
}
}
12 changes: 6 additions & 6 deletions Radzen.Blazor/QueryableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ private static string GetColumnFilter<T>(RadzenDataGridColumn<T> column, string
{
string filterCaseSensitivityOperator = column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ? ".ToLower()" : "";
value = value?.Replace("\"", "\\\"");

if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.Contains)
{
return $@"({property} == null ? """" : {property}){filterCaseSensitivityOperator}.Contains(""{value}""{filterCaseSensitivityOperator})";
Expand Down Expand Up @@ -950,7 +950,7 @@ public static IQueryable<T> Where<T>(this IQueryable<T> source, IEnumerable<Radz
}
}

return source.Where(string.Join($" {gridBooleanOperator} ", whereList.Keys), whereList.Values.SelectMany(i => i.ToArray()).ToArray());
return source.Where(DynamicLinqCustomTypeProvider.ParsingConfig, string.Join($" {gridBooleanOperator} ", whereList.Keys), whereList.Values.SelectMany(i => i.ToArray()).ToArray());
}

return source;
Expand Down Expand Up @@ -1053,13 +1053,13 @@ public static IQueryable<T> Where<T>(this IQueryable<T> source, IEnumerable<Radz
}
else if (comparison == "In" || comparison == "NotIn")
{
if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) &&
if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) &&
IsEnumerable(column.PropertyType) && column.PropertyType != typeof(string))
{
whereList.Add($@"{(comparison == "NotIn" ? "!" : "")}{property}.Any(i => i in @{index})", new object[] { column.GetFilterValue() });
index++;
}
else if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) &&
else if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) &&
column.Property != column.FilterProperty && !string.IsNullOrEmpty(column.FilterProperty))
{
whereList.Add($@"{(comparison == "NotIn" ? "!" : "")}{column.Property}.Any(i => i.{column.FilterProperty} in @{index})", new object[] { column.GetFilterValue() });
Expand Down Expand Up @@ -1109,7 +1109,7 @@ public static IQueryable<T> Where<T>(this IQueryable<T> source, IEnumerable<Radz
}

return whereList.Keys.Any() ?
source.Where(string.Join($" {gridBooleanOperator} ", whereList.Keys), whereList.Values.SelectMany(i => i.ToArray()).ToArray())
source.Where(DynamicLinqCustomTypeProvider.ParsingConfig, string.Join($" {gridBooleanOperator} ", whereList.Keys), whereList.Values.SelectMany(i => i.ToArray()).ToArray())
: source;
}

Expand Down Expand Up @@ -1143,7 +1143,7 @@ public static IQueryable<T> Where<T>(this IQueryable<T> source, RadzenDataFilter
}

return filterExpressions.Any() ?
source.Where(string.Join($" {dataFilter.LogicalFilterOperator.ToString().ToLower()} ", filterExpressions), filterValues.SelectMany(i => i.ToArray()).ToArray())
source.Where(DynamicLinqCustomTypeProvider.ParsingConfig, string.Join($" {dataFilter.LogicalFilterOperator.ToString().ToLower()} ", filterExpressions), filterValues.SelectMany(i => i.ToArray()).ToArray())
: source;
}

Expand Down
1 change: 0 additions & 1 deletion Radzen.Blazor/RadzenAutoComplete.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@using Radzen
@using Radzen.Blazor.Rendering
@using System.Collections
@using System.Linq.Dynamic.Core
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.JSInterop

Expand Down
2 changes: 1 addition & 1 deletion Radzen.Blazor/RadzenAutoComplete.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected override IEnumerable View

string textProperty = string.IsNullOrEmpty(TextProperty) ? string.Empty : $".{TextProperty}";

return Query.Where($"o=>o{textProperty}{filterCaseSensitivityOperator}.{Enum.GetName(typeof(StringFilterOperator), FilterOperator)}(@0)",
return Query.Where(DynamicLinqCustomTypeProvider.ParsingConfig, $"o=>o{textProperty}{filterCaseSensitivityOperator}.{Enum.GetName(typeof(StringFilterOperator), FilterOperator)}(@0)",
FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ? searchText.ToLower() : searchText);
}

Expand Down
4 changes: 2 additions & 2 deletions Radzen.Blazor/RadzenDataFilterProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ public virtual IEnumerable<FilterOperator> GetFilterOperators()
if (PropertyAccess.IsNullableEnum(FilterPropertyType))
return new FilterOperator[] { FilterOperator.Equals, FilterOperator.NotEquals, FilterOperator.IsNull, FilterOperator.IsNotNull };

if ((typeof(IEnumerable).IsAssignableFrom(FilterPropertyType) || typeof(IEnumerable<>).IsAssignableFrom(FilterPropertyType))
if ((typeof(IEnumerable).IsAssignableFrom(FilterPropertyType) || typeof(IEnumerable<>).IsAssignableFrom(FilterPropertyType))
&& FilterPropertyType != typeof(string))
{
var operators = new FilterOperator[]
var operators = new FilterOperator[]
{
FilterOperator.Contains,
FilterOperator.DoesNotContain,
Expand Down
1 change: 0 additions & 1 deletion Radzen.Blazor/RadzenDataGrid.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@using System.Linq.Dynamic.Core
@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Forms
@using Radzen
Expand Down
11 changes: 2 additions & 9 deletions Radzen.Blazor/RadzenDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ namespace Radzen.Blazor
#endif
public partial class RadzenDataGrid<TItem> : PagedDataBoundComponent<TItem>
{
class DynamicLinqCustomTypeProvider : System.Linq.Dynamic.Core.CustomTypeProviders.IDynamicLinkCustomTypeProvider
{
static readonly HashSet<Type> empty = [];
public HashSet<Type> GetCustomTypes() => empty;
public Dictionary<Type, List<System.Reflection.MethodInfo>> GetExtensionMethods() => throw new NotSupportedException();
public Type ResolveType(string typeName) => throw new NotSupportedException();
public Type ResolveTypeBySimpleName(string simpleTypeName) => throw new NotSupportedException();
}


#if NET5_0_OR_GREATER
/// <summary>
Expand Down Expand Up @@ -344,7 +337,7 @@ public IEnumerable<GroupResult> GroupedPagedView
if (_groupedPagedView == null)
{
var orderBy = GetOrderBy();
var query = Groups.Count(g => g.SortOrder == null) == Groups.Count || !string.IsNullOrEmpty(orderBy) ? View : View.OrderBy(new ParsingConfig() { CustomTypeProvider = new DynamicLinqCustomTypeProvider() }, string.Join(',', Groups.Select(g => $"{(typeof(TItem) == typeof(object) ? g.Property : "np(" + g.Property + ")")} {(g.SortOrder == null ? "" : g.SortOrder == SortOrder.Ascending ? " asc" : " desc")}")));
var query = Groups.Count(g => g.SortOrder == null) == Groups.Count || !string.IsNullOrEmpty(orderBy) ? View : View.OrderBy(DynamicLinqCustomTypeProvider.ParsingConfig, string.Join(',', Groups.Select(g => $"{(typeof(TItem) == typeof(object) ? g.Property : "np(" + g.Property + ")")} {(g.SortOrder == null ? "" : g.SortOrder == SortOrder.Ascending ? " asc" : " desc")}")));
var v = (AllowPaging && !LoadData.HasDelegate ? query.Skip(skip).Take(PageSize) : query).ToList().AsQueryable();
_groupedPagedView = v.GroupByMany(Groups.Select(g => $"{(typeof(TItem) == typeof(object) ? g.Property : "np(" + g.Property + ")")}").ToArray()).ToList();
}
Expand Down
8 changes: 4 additions & 4 deletions Radzen.Blazor/RadzenDataGridColumn.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public string GetFilterPlaceholder()
{
return FilterPlaceholder ?? string.Empty;
}

/// <summary>
/// Gets or sets the second filter value.
/// </summary>
Expand Down Expand Up @@ -922,7 +922,7 @@ public override async Task SetParametersAsync(ParameterView parameters)
return;
}
}

if (parameters.DidParameterChange(nameof(FilterOperator), FilterOperator))
{
filterOperator = parameters.GetValueOrDefault<FilterOperator>(nameof(FilterOperator));
Expand Down Expand Up @@ -980,7 +980,7 @@ internal IEnumerable GetFilterValues()
property = $@"({property} == null ? """" : {property})";
}

filterValues = Grid.Data.AsQueryable().Select(property).Distinct().Cast(propertyType ?? typeof(object));
filterValues = Grid.Data.AsQueryable().Select(DynamicLinqCustomTypeProvider.ParsingConfig, property).Distinct().Cast(propertyType ?? typeof(object));
}

return filterValues;
Expand Down Expand Up @@ -1240,7 +1240,7 @@ public virtual IEnumerable<FilterOperator> GetFilterOperators()
var isStringOperator = o == FilterOperator.Contains || o == FilterOperator.DoesNotContain
|| o == FilterOperator.StartsWith || o == FilterOperator.EndsWith || o == FilterOperator.IsEmpty || o == FilterOperator.IsNotEmpty;
if ((FilterPropertyType == typeof(string) || !QueryableExtension.IsEnumerable(FilterPropertyType)) &&
if ((FilterPropertyType == typeof(string) || !QueryableExtension.IsEnumerable(FilterPropertyType)) &&
(o == FilterOperator.In || o == FilterOperator.NotIn)) return false;
return FilterPropertyType == typeof(string) || QueryableExtension.IsEnumerable(FilterPropertyType) ? isStringOperator
Expand Down
4 changes: 2 additions & 2 deletions Radzen.Blazor/RadzenDataGridGroupFooterRow.razor
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

GroupResult _groupResult;
[Parameter]
public GroupResult GroupResult
{
public GroupResult GroupResult
{
get
{
return _groupResult;
Expand Down
1 change: 0 additions & 1 deletion Radzen.Blazor/RadzenDropDown.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@using Radzen
@using System.Linq
@using System.Linq.Dynamic.Core
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Rendering
Expand Down
1 change: 0 additions & 1 deletion Radzen.Blazor/RadzenDropDownDataGrid.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@using Radzen
@using System.Collections
@using System.Collections.Generic
@using System.Linq.Dynamic.Core
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.JSInterop
@typeparam TValue
Expand Down

0 comments on commit bd1a5ef

Please sign in to comment.