使用字符串访问列表中的子属性

时间:2016-01-06 14:39:04

标签: c#

我正在编写一个扩展方法,允许我使用字符串而不是Lambda表达式对IEnumerable列表对象执行OrderBy。它适用于简单的属性。但是,我正在试图弄清楚如何允许嵌套属性。

如果我的模特看起来像这样:

public class Submission
{
    public int SubmissionId {get; set;}
    public string Description {get; set;}
    public int ProjectId {get; set;}
    // Parent object
    public Project ParentProject {get; set;}
}

public class Project
{
    public int ProjectId {get; set;}
    public string FullTitle {get; set;}
}

我可以使用它来执行OrderBy:

public static class MkpExtensions
{
    public static IEnumerable<T> OrderByField<T>(this IEnumerable<T> list, string sortExpression)
    {
        sortExpression += "";
        string[] parts = sortExpression.Split(' ');
        bool descending = false;
        string fullProperty = "";

        if (parts.Length > 0 && parts[0] != "")
        {
            fullProperty = parts[0];

            if (parts.Length > 1)
            {
                descending = parts[1].ToLower().Contains("esc");
            }

             ParameterExpression inputParameter = Expression.Parameter(typeof(T), "p");
            Expression propertyGetter = inputParameter;
            foreach (string propertyPart in fullProperty.Split('.'))
            {
                PropertyInfo prop = propertyGetter.Type.GetProperty(propertyPart);
                if (prop == null)
                    throw new Exception("No property '" + fullProperty + "' in + " + propertyGetter.Type.Name + "'");
                propertyGetter = Expression.Property(propertyGetter, prop);
            }

            // This line was needed
            Expression conversion = Expression.Convert(propertyGetter, typeof(object));
            var getter = Expression.Lambda<Func<T, object>>(propertyGetter, inputParameter).Compile();

            if (descending)
                return list.OrderByDescending(x => prop.GetValue(x, null));
            else
                return list.OrderBy(x => prop.GetValue(x, null));
        }

        return list;
    }
}

我的代码会有这个:

public List<Submission> SortedSubmissions (bool simple = true) {
    var project1 = new Project { ProjectId = 1, FullTitle = "Our Project"};
    var project2 = new Project { ProjectId = 2, FullTitle = "A Project"};

    List<Submission> listToSort = new List<Submission> 
    {
        new Submission { SubmissionId = 1, Description = "First Submission", 
                        ProjectId = project1.ProjectId, ParentProject = project1 } ,
        new Submission { SubmissionId = 2, Description = "Second Submission", 
                        ProjectId = project1.ProjectId, ParentProject = project1 } ,
        new Submission { SubmissionId = 3, Description = "New Submission", 
                        ProjectId = project2.ProjectId, ParentProject = project2 }
    };

    var simpleField = "Description";
    // This would have the submissions sorted (1, 3, 2)
    var simpleSort = listToSort.OrderByField(simpleField + " asc").ToList();


    // Need to see if I can get this to work
    var nestedField = "Project.FullTitle";
    // This would have the submissions sorted (3, 1, 2)
    return listToSort.OrderByField(nestedField + " asc").ToList();
}

我希望我能清楚地解释自己。可以这样做吗?

更新 :我使用了AndréKops代码并进行了上述调整,但收到此错误:System.Nullable'1[System.Int32]' cannot be used for return type 'System.Object'

2 个答案:

答案 0 :(得分:1)

这是代码中的一个相当大的变化,但表达式树是完美的:

public static class MkpExtensions
{
    public static IEnumerable<T> OrderByField<T>(this IEnumerable<T> list, string sortExpression)
    {
        sortExpression += "";
        string[] parts = sortExpression.Split(' ');
        bool descending = false;
        string fullProperty = "";

        if (parts.Length > 0 && parts[0] != "")
        {
            fullProperty = parts[0];

            if (parts.Length > 1)
            {
                descending = parts[1].ToLower().Contains("esc");
            }

            ParameterExpression inputParameter = Expression.Parameter(typeof(T), "p");
            Expression propertyGetter = inputParameter;
            foreach (string propertyPart in fullProperty.Split('.'))
            {
                PropertyInfo prop = propertyGetter.Type.GetProperty(propertyPart);
                if (prop == null)
                    throw new Exception("No property '" + fullProperty + "' in + " + propertyGetter.Type.Name + "'");
                propertyGetter = Expression.Property(propertyGetter, prop);
            }

            Expression conversion = Expression.Convert(propertyGetter, typeof(object));
            var getter = Expression.Lambda<Func<T, object>>(conversion, inputParameter).Compile();

            if (descending)
                return list.OrderByDescending(getter);
            else
                return list.OrderBy(getter);
        }

        return list;
    }
}

此示例还允许嵌套深于2个属性。

对于大型列表来说,它可能更快。

答案 1 :(得分:0)

这是怎么回事?

    public static IEnumerable<T> OrderByField<T>(this IEnumerable<T> list, string sortExpression)
    {
        sortExpression += "";
        string[] parts = sortExpression.Split(' ');
        bool descending = false;
        string fullProperty = "";

        if (parts.Length > 0 && parts[0] != "")
        {
            fullProperty = parts[0];

            if (parts.Length > 1)
            {
                descending = parts[1].ToLower().Contains("esc");
            }

            string fieldName;

            PropertyInfo parentProp = null;
            PropertyInfo prop = null;

            if (fullProperty.Contains("."))
            {
                // A nested property
                var parentProperty = fullProperty.Remove(fullProperty.IndexOf("."));
                fieldName = fullProperty.Substring(fullProperty.IndexOf("."));

                parentProp = typeof(T).GetProperty(parentProperty);
                prop = parentProp.PropertyType.GetProperty(fieldName);
            }
            else
            {
                // A simple property
                prop = typeof(T).GetProperty(fullProperty);
            }

            if (prop == null)
            {
                throw new Exception("No property '" + fullProperty + "' in + " + typeof(T).Name + "'");
            }

            if (parentProp != null)
            {
                if (descending)
                    return list.OrderByDescending(x => prop.GetValue(parentProp.GetValue(x, null), null));
                else
                    return list.OrderBy(x => prop.GetValue(parentProp.GetValue(x, null), null));
            }
            else
            {
                if (descending)
                    return list.OrderByDescending(x => prop.GetValue(x, null));
                else
                    return list.OrderBy(x => prop.GetValue(x, null));
            }
        }

        return list;
    }