3

我有一个场景,我需要获取类属性的值集合。

public class Person
{
    public string Name { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class PersonCollection : List<Person>
{
    public object[] GetValues(string propertyName)
    {
        // best way to implement this method?
        return null;
    }
}

我想避免大量的迭代。任何想法都会有所帮助。

4

7 回答 7

4

A bit of Linq magic:

public object[] GetValues(Expression<Func<Person, object>> exp)
{
    var function = exp.Compile();
    return this.Select(function).ToArray();
}

Usage:

// assuming coll in a PersonCollection
var names = coll.GetValues(p => p.Name);
于 2013-06-06T10:15:23.577 回答
2

一个简单的解决方案是使用 LINQ 的Select方法:

using System;
using System.Collections.Generic;
using System.Linq;

public class Person
{
    public string Name { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class PersonCollection : List<Person>
{
    public object[] GetValues(string propertyName)
    {
        if (propertyName == "Name")
        {
            return this.Select(p => p.Name).ToArray();
        }
        else if (propertyName == "Property1")
        {
            return this.Select(p => p.Property1).ToArray();
        }
        else if (propertyName == "Property2")
        {
            return this.Select(p => p.Property1).ToArray();
        }

        // best way to implement this method?
        return null;
    }
}

您还可以使用表达式树来允许将类型安全的访问器 lambda 用作参数:

public object[] GetValues(Expression<Func<Person, object>> propertyNameExpression)
{
    var compiledPropertyNameExpression = propertyNameExpression.Compile();

    if (propertyNameExpression.Body.NodeType == ExpressionType.MemberAccess)
    {
        return this.Select(compiledPropertyNameExpression).ToArray();
    }

    throw new InvalidOperationException("Invalid lambda specified. The lambda should select a property.");
}

然后,您可以按如下方式使用它:

var personNames = personCollection.GetValues(p => p.Name)
于 2013-06-06T10:09:01.027 回答
2

一个不使用反射的简单想法是这样的:

public partial class PersonCollection: List<Person> {
    public object[] GetValues(String propertyName) {
        return (
            from it in this
            let x=
                "Name"==propertyName
                    ?it.Name
                    :"Property1"==propertyName
                        ?it.Property1
                        :"Property2"==propertyName
                            ?it.Property2
                            :default(object)
            where null!=x
            select x).ToArray();
    }
}

但我宁愿返回一个IEnumerablefor 不要急于列举:

public partial class PersonCollection: List<Person> {
    public IEnumerable GetValues(String propertyName) {
        return
            from it in this
            let x=
                "Name"==propertyName
                    ?it.Name
                    :"Property1"==propertyName
                        ?it.Property1
                        :"Property2"==propertyName
                            ?it.Property2
                            :default(object)
            where null!=x
            select x;
    }
}
于 2013-06-06T10:24:58.717 回答
1

Here's a working program

public class Person
{
    public string Name { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class PersonCollection : List<Person>
{
    public object[] GetValues(string propertyName)
    {
        var result = new List<object>();
        foreach (Person item in this)
        {
            result.Add(item.GetType().GetProperty(propertyName).GetValue(item));
        }
        return result.ToArray();
    }
}
class Program
{
    static void Main(string[] args)
    {
        var collection = new PersonCollection();
        collection.Add(new Person(){Name = "George", Property1 = "aaa", Property2 = "bbbb"});
        collection.Add(new Person(){Name = "Peter", Property1 = "ccc", Property2 = "dddd"});
        var objects = collection.GetValues("Property1");
        foreach (object item in objects)
        {
            Console.WriteLine(item.ToString());
        }
        Console.Read();
    }
}
于 2013-06-06T10:15:06.067 回答
1

使用反射

人 P = 新人();对象 obj = p.GetType().GetProperty(propertyName).GetValue(p, null);

于 2013-06-06T10:09:08.093 回答
1

尝试这个,

public object[] GetValues(string propertyName)
{
    List<object> result = new List<object>();
    PropertyInfo propertyInfo = typeof(Person).GetProperty(propertyName);
    this.ForEach(person => result.Add(propertyInfo.GetValue(person)));
    return result.ToArray();
}
于 2013-06-06T10:12:02.117 回答
0

如果您正在使用实体框架,则可以使用 this.GetAll();

于 2013-06-06T10:07:12.737 回答