2

I read almost topic about dynamic LINQ as: + C# Dynamic linq order by expando object + build linq queries dynamically ... However, my list contains expando Object. So i can't apply dynamic linq as:

 query = QueryJoinCombo.AsQueryable().Where(where.ToUpper()).OrderBy(orderBy);

Because dynamic linq can not get properties list of 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);
        }

I think we can modify dynamic linq to get properties of expandoObject type. I wrote a method to get value of 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;
        }

However, I don't know how to apply to dynamic class. Does anyone can give me a solution for this issue? or suggest for me a way? Thank for reading.

Community
  • 1
  • 1
mrfour.it
  • 31
  • 3

0 Answers0