动态返回不同的值
本文关键字:返回 动态 | 更新日期: 2023-09-27 18:02:26
我有一个这样结构的类:
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();
现在工作得很好…但如果我不知道用户想要不同值的属性呢?如果我所有的是一个字符串变量,告诉我哪个列他们想要不同的值??
Try
List<object> test = (from x in BusinessObjectCollection select x.GetType().GetProperty ("thePropertyName").GetGetMethod().Invoke(x,null)).Distinct().ToList();
我将使用if
。为什么要把这复杂化呢?
试试这样的东西伪代码!:
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();
}
。
问候。