Ecco il problema:
Usiamo l'oggetto tabella per consentire agli utenti di eseguire alcune funzionalità come ricerca, ordinamento, impaginazione, ecc. Queste tabelle funzionano alla grande. Ma c'è un problema con una delle funzionalità: The sort (= OrderBy).
Infatti, per consentire l'ordinamento, impostiamo in ogni colonna una stringa che rappresenta l'espressione: Ad esempio, se l'espressione è Person => Person.Id, la stringa è Id; se l'espressione è Person => Person.Address.Street, la stringa è Address.Street.
Nel primo caso (Person => Person.Id), funziona alla grande perché non è un oggetto secondario. Ma nel secondo caso (Person => Person.Address.Street), ciò non avviene poiché l'oggetto Address potrebbe essere nullo.
Per consentire che Orderby venga eseguito da una stringa, ho trovato su un altro post i seguenti metodi:
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
private static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
Qualcuno di voi ha un'idea che mi permetterebbe di aggiungere una condizione che non selezionerebbe l'oggetto con subobject == null? O impedirgli di provare ad accedere ad una proprietà da un oggetto che è nullo?
MODIFICARE :
Il controllo sarebbe simile a: list.OrderBy (x => (x.Address! = Null)? X.Address.Street: string.Empty).
Quindi ho bisogno di aggiungere un controllo nullo su ogni oggetto tra x e il campo finale. È possibile farlo usando quei metodi?
MODIFICA 2:
Ho provato a sostituire
Expression.Property(expr, pi);
di
expr = Expression.Condition(
Expression.Equal(expr, Expression.Constant(null)),
Expression.Constant(String.Empty),
Expression.Property(expr, pi));
Ma sembra che non funzioni. Ottengo la seguente eccezione:
Argument types do not match
Qualche idea su come dovrei conoscere il valore predefinito per il campo a cui accede l'expr?
crea la tua costante nulla come il tipo di proprietà.
expr = Expression.Condition(Expression.Equal(expr, Expression.Constant(null, expr.Type)),
Expression.Constant(String.Empty),
Expression.Property(expr, pi));