我几乎读到了关于动态LINQ的主题: + C# Dynamic linq order by expando object + build linq queries dynamically ... 但是,我的列表包含expando Object。所以我不能将动态linq应用为:
query = QueryJoinCombo.AsQueryable().Where(where.ToUpper()).OrderBy(orderBy);
因为动态linq无法获取ExpandoObject的属性列表。
//Method of dynamic linq
public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (ordering == null) throw new ArgumentNullException("ordering");
ParameterExpression[] parameters = new ParameterExpression[] {
Expression.Parameter(source.ElementType, "") };
ExpressionParser parser = new ExpressionParser(parameters, ordering, values);
IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
Expression queryExpr = source.Expression;
string methodAsc = "OrderBy";
string methodDesc = "OrderByDescending";
foreach (DynamicOrdering o in orderings)
{
queryExpr = Expression.Call(
typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
new Type[] { source.ElementType, o.Selector.Type },
queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters)));
methodAsc = "ThenBy";
methodDesc = "ThenByDescending";
}
return source.Provider.CreateQuery(queryExpr);
}
我认为我们可以修改动态linq来获取expandoObject类型的属性。我写了一个方法来获取Expand Object的值:
public static string GetValueInExpandoObject(dynamic objInput, string propertyName)
{
IDictionary<string, object> dicInput = (IDictionary<string, object>)objInput;
string strResult = "Default";
if (dicInput.ContainsKey(propertyName) == true)
strResult = dicInput[propertyName].ToString();
return strResult;
}
但是,我不知道如何申请动态课程。 有人能为我解决这个问题吗?还是建议我一个方法? 感谢阅读。