2011-07-19 37 views
0

我有這個結構的類:返回不同的值動態

public class BusinessObject 
{ 
    public int Column5 { get; set; } 
    public int Column6 { get; set; } 
    public string Column7 { get; set; } 
    public int Column8 { get; set; } 
} 

,我必須使用LINQ這一行:

List<int> test = (from x in BusinessObjectCollection select x.Column5).Distinct().ToList(); 

現在這個工作不錯..但如果我不該做什麼知道用戶需要不同值的屬性?如果我擁有的是一個字符串變量,告訴我他們想從哪個列中獲取不同的值?

回答

2

嘗試

List<object> test = (from x in BusinessObjectCollection select x.GetType().GetProperty ("thePropertyName").GetGetMethod().Invoke(x,null)).Distinct().ToList(); 
0

我會用if。爲什麼複雜呢?

0

嘗試像這樣的事情pseudeocode!

IQueryable<BusinessObject> filteredBoList = boList.AsQueryable(); 
Type boType= typeof(BusinessObject); 
foreach(string filter in filterColumnNames) { 
    var found = filteredBoList.Where(p => (string)boType.InvokeMember(filter.FieldName,BindingFlags.GetProperty, null, p, null) == filter).Distinct(); 
} 

應該工作。

問候。