【问题标题】:Net Core: Find Data Type for any Variable Field in ClassNet Core:查找类中任何变量字段的数据类型
【发布时间】:2019-12-28 06:26:27
【问题描述】:

如何获取这个 OrderBy 表达式树并使其接受任何 Order 类型、int、float、string、boolean 等?可能吗?现在它的类型转换为仅字符串。我应该在调用方法时将所有内容都转换为字符串,还是有更好的方法来使其更通用?

我只需要T类中这个propertyName的数据类型,所以我可以放在下面的函数中。测试这些,还没有运气。

会员类型, 获取类型(), 字段类型

OrderBy Expression Tree in Net Core Linq for Extension Method

创建表达式:

public static class ExpressionTreesExtesion
{
    public static Expression<Func<T,string>> OrderByExpression<T>(this IEnumerable<T> enumerable, string propertyName)
    {
        var propInfo = typeof(T).GetProperty(propertyName);

        var collectionType = typeof(T);

        var parameterExpression = Expression.Parameter(collectionType, "x");
        var propertyAccess = Expression.MakeMemberAccess(parameterExpression, propInfo);
        var orderExpression = Expression.Lambda<Func<T,string>>(propertyAccess, parameterExpression);
        return orderExpression;
    }
}

如何调用:

var ProductExpression = records.OrderByExpression("Name");

var result  = records.OrderBy(ProductExpression.Compile());
ProductExpression.Compile() above will compile into x => x.Name, where column name is supplied at the run-time

【问题讨论】:

  • 用 Func 替换 Func 并修复产生的错误。
  • @RandomUs1r 它告诉我生成空字段类,只需要T类中这个propertyName的数据类型,所以我可以放在下面的函数中,测试这些MemberType、GetType()、FieldType跨度>
  • @RandomUs1r Propertyname 将始终是一个字符串,但它可以引用可能具有不同数据类型的不同字段(Product 是 int,ProductName 是 string,Productamount 是 float 等)

标签: c# .net linq .net-core expression-trees


【解决方案1】:

因为在编译时类型是未知的,所以您将无法使用像 Expression&lt;Func&lt;T,TKey&gt;&gt; 这样的强类型返回类型。

public static class ExpressionTreesExtension {
    static readonly Type funcTTResult = typeof(Func<,>);
    public static IOrderedQueryable<T> OrderByProperty<T>(this IEnumerable<T> enumerable, string propertyName) {
        var itemType = typeof(T);
        var propertyInfo = itemType.GetProperty(propertyName);
        var propertyType = propertyInfo.PropertyType;
        // Func<T,TPropertyType>
        var delegateType = funcTTResult.MakeGenericType(itemType, propertyType);
        // T x =>
        var parameterExpression = Expression.Parameter(itemType, "x");
        // T x => x.Property
        var propertyAccess = Expression.Property(parameterExpression, propertyInfo);
        // Func<T,TPropertyType> = T x => x.Property
        var keySelector = Expression.Lambda(delegateType, propertyAccess, parameterExpression);

        var query = enumerable.AsQueryable();

        // query.OrderBy(x => x.Property)
        MethodCallExpression orderByExpression = Expression.Call(
             typeof(Queryable),
             "OrderBy",
             new[] { query.ElementType, propertyInfo.PropertyType },
             query.Expression, keySelector);

        // Create an executable query from the expression tree. 
        return (IOrderedQueryable<T>)query.Provider.CreateQuery<T>(orderByExpression);
    }
}

参考How to: Use Expression Trees to Build Dynamic Queries (C#)

和使用一样

//IEnumerable<Person> records...
var data = records.OrderByProperty("Name");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2016-10-18
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多