使用字符串和嵌套反射的EntityFramework LINQ订单 [英] EntityFramework LINQ Order using string and nested reflection

查看:100
本文介绍了使用字符串和嵌套反射的EntityFramework LINQ订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些类似的问题,但是找不到答案解决该问题的方法。

我希望有可能使用字符串作为属性名称来订购集合。

I saw already some of the similar questions, but cannot find an anwser how to solve this problem.
I want to have a possibility to order collection using string as property name.

型号:

public sealed class User : IdentityUser
{
    #region Constructors

    #endregion

    #region Properties

    [Required]
    [Display(Name = "First name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last name")]
    public string LastName { get; set; }

    [ForeignKey("Superior")]
    public string SuperiorId { get; set; }

    public User Superior{ get; set; }

    [NotMapped]
    public string FullName => this.LastName + " " + this.FirstName;

    #endregion
}

我想做一个这样的呼叫:

And I want to make a call like this:

DbSet.Order("SuperiorUser.FullName", Asc/Desc)...

我的以下函数可用于 Order( FullName,Asc / Desc)

My below functions work for a Order("FullName", Asc/Desc), but dont when I want to go deeper.

public static IOrderedQueryable<T> Order<T>(this IQueryable<T> source, string propertyName,
    ListSortDirection direction = ListSortDirection.Ascending)
{
    return ListSortDirection.Ascending == direction
        ? source.OrderBy(ToLambda<T>(propertyName))
        : source.OrderByDescending(ToLambda<T>(propertyName));
}

private static Expression<Func<T, object>> ToLambda<T>(string propertyName)
{
    var parameter = Expression.Parameter(typeof(T));
    var property = Expression.Property(parameter, propertyName);

    return Expression.Lambda<Func<T, object>>(property, parameter);
}

所以我使它看起来像这样

So I made it look a little bit like this

private static Expression<Func<T, object>> ToLambda<T>(string propertyName)
{
    var propertyNames = propertyName.Split('.');
    var type = typeof(T)
    ParameterExpression parameter;
    MemberExpression property;
    for (var propName in propertyNames)
    {
        parameter = Expression.Parameter(type);
        property = Expression.Property(parameter, propName);
        type = property.Type;
    }
    return Expression.Lambda<Func<T, object>>(property, parameter);
}

但不幸的是,此错误返回错误参数未绑定到指定的LINQ to Entities查询表达式。我缺少什么?

But this unfortunately returns error The parameter '' was not bound in the specified LINQ to Entities query expression. What am I missing?

推荐答案

您需要嵌套 Property 访问权限

private static Expression<Func<T, object>> ToLambda<T>(string propertyName) {
    var propertyNames = propertyName.Split('.');
    var parameter = Expression.Parameter(typeof(T));
    Expression body = parameter;
    foreach (var propName in propertyNames)
        body = Expression.Property(body, propName);
    return Expression.Lambda<Func<T, object>>(body, parameter);
}

这篇关于使用字符串和嵌套反射的EntityFramework LINQ订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆