Skip to content

Commit

Permalink
use case insensitive properties for search on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-schick committed Mar 17, 2024
1 parent 26f8f69 commit b0f892a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/BccCode.Linq/Server/CollectionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public static IQueryable<T> ApplyRuleFilter<T>(this IQueryable<T> source, Filter
internal static IEnumerable<(PropertyInfo, ListSortDirection)> GetSorting<T>(string sort)
where T : class
{
var propertyMetadata = typeof(T)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

foreach (var sortByField in sort.Split(','))
{
if (sortByField.Contains('.'))
Expand Down Expand Up @@ -71,8 +74,10 @@ public static IQueryable<T> ApplyRuleFilter<T>(this IQueryable<T> source, Filter
}

// try find property by camel case
var propertyInfo = typeof(T).GetProperty(propertyName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
//var propertyInfo = typeof(T).GetProperty(propertyName,
// BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
// try find case insensitive property by name
var propertyInfo = propertyMetadata.FirstOrDefault(x => string.Equals(x.Name, propertyName, StringComparison.CurrentCultureIgnoreCase));

if (propertyInfo == null)
{
Expand Down Expand Up @@ -101,7 +106,7 @@ public static IQueryable<T> ApplyRuleFilter<T>(this IQueryable<T> source, Filter
yield return (propertyInfo, sortDirection);
}
}

/// <summary>
/// Applies query parameters to a <seealso cref="IQueryable"/> object.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions src/BccCode.Linq/Server/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using BccCode.Linq;
using BccCode.Linq.Server;
*/
using System.Reflection;
using BccCode.Linq.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -87,11 +88,12 @@ private void _parse(string json)
{
var deserializedJson = FilterDeserializationHelpers.DeserializeJsonRule(json);

var propertyMetadata = typeof(T).GetProperties();
var propertyMetadata = typeof(T)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

foreach (var (key, value) in deserializedJson)
{
var propertyInfo = propertyMetadata.FirstOrDefault(x => x.Name.ToLower() == key.ToLower());
var propertyInfo = propertyMetadata.FirstOrDefault(x => string.Equals(x.Name, key, StringComparison.CurrentCultureIgnoreCase));

if (new[] { "_and", "_or" }.Contains(key))
{
Expand Down

0 comments on commit b0f892a

Please sign in to comment.