public void TEST(SEPM.SEPMDataContext oDC)
{
//一般 LINQ 寫法, 欄位只能寫死, 如:
var Q = query.OrderBy(r => r.statusId).ToList();
//若 OrderBy 的欄位不是固定的
//方法一:條列
var orderBy = "XXX";
if (orderBy == "ownerId")
{
Q = Q.OrderBy(r => r.ownerId).ToList();
}
else if (orderBy == "endDate")
{
Q = Q.OrderBy(r => r.endDate).ToList();
}
//方法二:
Q = Q.OrderBy(r => GetPropertyValue(r, orderBy)).ToList();
}
private object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
參考:http://coderemind.blogspot.tw/2013/10/linq.html