Cos'è equivalente a GetPropValue < T > metodo in L2E?
-
05-07-2019 - |
Domanda
Ho un problema con l'utilizzo di EF per il mio modello di dati.
Ho questo codice nel mio metodo:
listaPaginada = sortOrder.Equals("asc") ?
_cadastroServ.SelecionaNotasFiscais(idParceiro).OrderBy(i => i.GetType().GetProperty(query)) :
_cadastroServ.SelecionaNotasFiscais(idParceiro).OrderByDescending(i => i.GetType().GetProperty(query));
i & # 180; m usando lo stesso metodo ascolta:
Func<NotaFiscal, bool> whereClause = (i => i.GetPropValue<string>(sortName).Contains(query));
listaPaginada = sortOrder.Equals("asc", StringComparison.CurrentCultureIgnoreCase) ?
_cadastroServ.SelecionaNotasFiscais(idParceiro).Where(whereClause).OrderByDescending(i => i.GetPropValue<IComparable>(sortName)) :
_cadastroServ.SelecionaNotasFiscais(idParceiro).Where(whereClause).OrderBy(i => i.GetPropValue<IComparable>(sortName));
In L2SQL esiste il metodo GetPropValue, ma in L2E no.
Qualcuno conosce un metodo simile in L2E? o sa come risolverlo?
Saluti []
Soluzione
Non ho familiarità con GetPropValue
in LINQ-to-SQL. Sei sicuro che esista? Detto questo, è abbastanza facile evocare cosa sta facendo questo metodo e scrivere di conseguenza:
static class ObjectExtensions {
public static T GetPropValue<T>(this object value, string propertyName) {
if (value == null) { throw new ArgumentNullException("value"); }
if (String.IsNullOrEmpty(propertyName)) { throw new ArgumentException("propertyName"); }
PropertyInfo info = value.GetType().GetProperty(propertyName);
return (T)info.GetValue(value, null);
}
}
Utilizzo:
public class Test {
public string Name { get; set; }
public int Number { get; set; }
}
Test test = new Test() { Name = "Jenny", Number = "8675309" };
Console.WriteLine(test.GetPropValue<string>("Name"));
Console.WriteLine(test.GetPropValue<int>("Number"));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow