动态返回不同的值

时间:2011-07-19 14:04:39

标签: c# wpf

我有一个具有这种结构的课程:

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

现在这很好..但如果我不知道用户想要不同值的属性怎么办?如果我拥有的是一个字符串变量,它告诉我他们想要哪个列来自???

的不同值

3 个答案:

答案 0 :(得分:2)

尝试

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

答案 1 :(得分:0)

我会使用if。为什么会这样复杂?

答案 2 :(得分:0)

尝试类似 伪代码的内容!

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

应该工作。

问候。

相关问题