2013-06-06 65 views
2

我有我需要獲取類屬性值集合的場景。如何從集合中獲取某個類的屬性列表

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

我想避免太多的迭代。任何想法都會有幫助。

回答

1

一個簡單的解決方案是一個使用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."); 
} 

然後,您可以將此用作follo WS:

var personNames = personCollection.GetValues(p => p.Name) 
+0

感謝您的解決方案。 –

+0

@DJ不客氣。 –

0

如果您正在使用實體框架, 您可以使用工作this.GetAll();

+0

。 –

0

使用反射

人P =新的Person(); object obj = p.GetType()。GetProperty(propertyName).GetValue(p,null);

0

試試這個,

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

這裏有一個工作程序

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

的LINQ的神奇了一下:

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

用法:

// assuming coll in a PersonCollection 
var names = coll.GetValues(p => p.Name); 
1

不使用反射一個簡單的想法是這樣的:

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

但我寧願回到IEnumerable不急於枚舉:不幸的是,我沒有使用

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; 
    } 
} 
相關問題