简体   繁体   English

等效的GetPropValue <T> L2E中的方法?

[英]What is equivalent GetPropValue<T> method in L2E?

I´m have a problem using EF for my data model. 使用数据模型的EF时遇到问题。

I have this code in my method: 我的方法中有以下代码:

  listaPaginada = sortOrder.Equals("asc") ?
                    _cadastroServ.SelecionaNotasFiscais(idParceiro).OrderBy(i =>   i.GetType().GetProperty(query)) :
                    _cadastroServ.SelecionaNotasFiscais(idParceiro).OrderByDescending(i => i.GetType().GetProperty(query));

i´m using the same method hear to: 我正在使用相同的方法听到:

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 the method GetPropValue exists, but in L2E not. 在L2SQL中,方法GetPropValue存在,但在L2E中不存在。

Someone knows a similar method in L2E ? 有人知道L2E中有类似的方法吗? or knows how to solve this ? 还是知道如何解决这个问题?

Regards[] 问候[]

I'm not familiar with GetPropValue in LINQ-to-SQL. 我对LINQ-to-SQL中的GetPropValue不熟悉。 Are you sure it exists? 您确定它存在吗? That said, it's easy enough to conjure up what this method is doing and write accordingly: 也就是说,可以很容易地弄清楚该方法在做什么并相应地编写:

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);
    }
}

Usage: 用法:

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM