Skip to content

Commit 62e8359

Browse files
committed
feat: 增加 CastAndOrder 方法
1 parent d0c4d9a commit 62e8359

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

src/BootstrapBlazor/Extensions/LambdaExtensions.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,24 @@ private static IEnumerable<TItem> EnumerableOrderBy<TItem>(IEnumerable<TItem> qu
449449

450450
IEnumerable<TItem> EnumerableOrderBySimple()
451451
{
452+
var type = typeof(TItem);
452453
IEnumerable<TItem>? ret = null;
453-
var pi = typeof(TItem).GetPropertyByName(propertyName);
454-
if (pi != null)
454+
if (type.IsInterface && type == typeof(IDynamicObject))
455455
{
456-
var methodName = sortOrder == SortOrder.Desc ? nameof(OrderByDescendingInternal) : nameof(OrderByInternal);
457-
ret = query.AsQueryable().InvokeSortByPropertyInfo(methodName, pi);
456+
var instance = query.FirstOrDefault();
457+
if (instance != null)
458+
{
459+
ret = CastAndOrder(query, instance.GetType(), propertyName, sortOrder);
460+
}
461+
}
462+
else
463+
{
464+
var pi = type.GetPropertyByName(propertyName);
465+
if (pi != null)
466+
{
467+
var methodName = sortOrder == SortOrder.Desc ? nameof(OrderByDescendingInternal) : nameof(OrderByInternal);
468+
ret = query.AsQueryable().InvokeSortByPropertyInfo(methodName, pi);
469+
}
458470
}
459471
return ret ?? query;
460472
}
@@ -476,6 +488,25 @@ IEnumerable<TItem> EnumerableOrderByComplex()
476488
}
477489
}
478490

491+
private static IEnumerable<TItem>? CastAndOrder<TItem>(IEnumerable<TItem> query, Type propertyType, string propertyName, SortOrder sortOrder)
492+
{
493+
IEnumerable<TItem>? ret = null;
494+
var castMethod = typeof(Enumerable).GetMethod(nameof(Enumerable.Cast), BindingFlags.Static | BindingFlags.Public);
495+
if (castMethod != null)
496+
{
497+
var mi = castMethod.MakeGenericMethod(propertyType);
498+
var collection = mi.Invoke(null, [query]);
499+
500+
var orderMethod = typeof(LambdaExtensions).GetMethod(nameof(EnumerableOrderBy), BindingFlags.Static | BindingFlags.NonPublic);
501+
if (orderMethod != null)
502+
{
503+
var miOrder = orderMethod.MakeGenericMethod(propertyType);
504+
ret = miOrder.Invoke(null, [collection, propertyName, sortOrder]) as IEnumerable<TItem>;
505+
}
506+
}
507+
return ret;
508+
}
509+
479510
private static IEnumerable<TItem> EnumerableThenBy<TItem>(IEnumerable<TItem> query, string propertyName, SortOrder sortOrder)
480511
{
481512
return propertyName.Contains('.') ? EnumerableThenByComplex() : EnumerableThenBySimple();
@@ -583,13 +614,13 @@ IQueryable<TItem> QueryableThenByComplex()
583614

584615
private static IOrderedQueryable<TItem> ThenByDescendingInternalByName<TItem, TKey>(IOrderedQueryable<TItem> query, string propertyName) => query.ThenByDescending(GetPropertyLambdaByName<TItem, TKey>(propertyName));
585616

586-
private static IOrderedQueryable<TItem> OrderByInternal<TItem, TKey>(IQueryable<TItem> query, System.Reflection.PropertyInfo memberProperty) => query.OrderBy(GetPropertyLambda<TItem, TKey>(memberProperty));
617+
private static IOrderedQueryable<TItem> OrderByInternal<TItem, TKey>(IQueryable<TItem> query, PropertyInfo memberProperty) => query.OrderBy(GetPropertyLambda<TItem, TKey>(memberProperty));
587618

588-
private static IOrderedQueryable<TItem> OrderByDescendingInternal<TItem, TKey>(IQueryable<TItem> query, System.Reflection.PropertyInfo memberProperty) => query.OrderByDescending(GetPropertyLambda<TItem, TKey>(memberProperty));
619+
private static IOrderedQueryable<TItem> OrderByDescendingInternal<TItem, TKey>(IQueryable<TItem> query, PropertyInfo memberProperty) => query.OrderByDescending(GetPropertyLambda<TItem, TKey>(memberProperty));
589620

590-
private static IOrderedQueryable<TItem> ThenByInternal<TItem, TKey>(IOrderedQueryable<TItem> query, System.Reflection.PropertyInfo memberProperty) => query.ThenBy(GetPropertyLambda<TItem, TKey>(memberProperty));
621+
private static IOrderedQueryable<TItem> ThenByInternal<TItem, TKey>(IOrderedQueryable<TItem> query, PropertyInfo memberProperty) => query.ThenBy(GetPropertyLambda<TItem, TKey>(memberProperty));
591622

592-
private static IOrderedQueryable<TItem> ThenByDescendingInternal<TItem, TKey>(IOrderedQueryable<TItem> query, System.Reflection.PropertyInfo memberProperty) => query.ThenByDescending(GetPropertyLambda<TItem, TKey>(memberProperty));
623+
private static IOrderedQueryable<TItem> ThenByDescendingInternal<TItem, TKey>(IOrderedQueryable<TItem> query, PropertyInfo memberProperty) => query.ThenByDescending(GetPropertyLambda<TItem, TKey>(memberProperty));
593624

594625
private static Expression<Func<TItem, TKey>> GetPropertyLambda<TItem, TKey>(PropertyInfo pi)
595626
{

0 commit comments

Comments
 (0)