使用Expression来构造,学习周期比较长
使用附件里面微软的开源类库Dynamic类,附件就是示例
不懂可以留联系。
public ListGetSortedEmployee(string sortField, bool isAsc)
{
Listemp = GetEmployee();
if (isAsc)
{
var res = from e in emp orderby GetPropertyValue(e, sortField) select e;
emp = res.ToList();
}
else
{
var res = from e in emp orderby GetPropertyValue(e, sortField) descending select e;
emp = res.ToList();
}
return emp;
}
private object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
不过这样只能把数据全读出来在筛选,就是在内存中对list操作
我不知道。