public PagingList <T> GetWithCriteria <T>(Criteria criteria, string includeProperties = null) where T : class
        {
            var toReturn     = new PagingList <T>();
            var query        = base.Set <T>().IncludePropertyListCsv(includeProperties);
            var queryBuilder = query as IQueryable <T>;

            if (criteria.FilterFieldName != null && criteria.FilterFieldValue != null)
            {
                queryBuilder = queryBuilder.Where(
                    DynamicExpressionBuilder.BuildFilterExpression <T>(
                        criteria.FilterFieldName,
                        criteria.FilterFieldValue,
                        StringFilterOperator.Contains));
            }

            toReturn.TotalCount = queryBuilder.Count();

            if (criteria.SortFieldName != null)
            {
                queryBuilder = queryBuilder.OrderBy(
                    property: criteria.SortFieldName,
                    direction: (criteria.SortDirection == SortDirection.Ascending) ? "ASC" : "DESC");
            }

            if (criteria.PageSize != -1)
            {
                queryBuilder = queryBuilder
                               .Skip(criteria.PageSize * (criteria.PageNumber - 1))
                               .Take(criteria.PageSize);
            }

            toReturn.AddRange(queryBuilder);

            return(toReturn);
        }
        public PagingList <T> GetWithCriteria <T>(Criteria criteria, string includeProperties = null) where T : class
        {
            var toReturn     = new PagingList <T>();
            var table        = _session.GetTable <T>();
            var queryBuilder = table as CqlQuery <T>;

            if (criteria.FilterFieldName != null && criteria.FilterFieldValue != null)
            {
                queryBuilder = queryBuilder.Where(
                    DynamicExpressionBuilder.BuildFilterExpression <T>(
                        criteria.FilterFieldName,
                        criteria.FilterFieldValue,
                        StringFilterOperator.Equals));
            }

            var select     = queryBuilder.Select(m => m);
            var expression = select.Expression;

            Type d1 = typeof(CqlQuery <>);

            Type[] typeArgs          = { typeof(T) };
            Type   typeToInstantiate = d1.MakeGenericType(typeArgs);


            var cqlQuery = Activator.CreateInstance(typeToInstantiate,
                                                    System.Reflection.BindingFlags.NonPublic |
                                                    System.Reflection.BindingFlags.Instance,
                                                    null, new object[] { expression, table }, null) as CqlQuery <T>;

            var records = cqlQuery.Execute().ToList();

            toReturn.TotalCount = records.Count;

            if (criteria.SortFieldName != null)
            {
                records = records.AsQueryable <T>().OrderBy(
                    property: criteria.SortFieldName,
                    direction: (criteria.SortDirection == SortDirection.Ascending) ? "ASC" : "DESC").ToList();
            }

            if (criteria.PageSize != -1)
            {
                records = records
                          .Skip(criteria.PageSize * (criteria.PageNumber - 1))
                          .Take(criteria.PageSize)
                          .ToList();
            }


            toReturn.AddRange(records);

            return(toReturn);
        }
        public static dynamic DeserializeArguments(Type type, Dictionary <string, dynamic> arguments)
        {
            var result = Activator.CreateInstance(type);

            var properties = type.GetProperties();

            foreach (var property in properties)
            {
                var argumentKey = arguments.Keys.FirstOrDefault(x => x.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase));

                if (argumentKey != null)
                {
                    var value = arguments[argumentKey];

                    // Nullable properties
                    if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        var args = property.PropertyType.GetGenericArguments();

                        // Nullable<Enum>
                        if (args.First().IsEnum)
                        {
                            property.SetValue(result, Enum.Parse(args.First(), value), null);
                        }
                        // Nullable<int>
                        if (args.First() == typeof(Int32))
                        {
                            property.SetValue(result, Int32.Parse(value), null);
                        }
                    }
                    // Handle expressions
                    else if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Expression <>))
                    {
                        var args     = property.PropertyType.GetGenericArguments();
                        var firstArg = args.First();

                        // Expression<Func<,>>
                        if (firstArg.IsGenericType && firstArg.GetGenericTypeDefinition() == typeof(Func <,>))
                        {
                            var paramArgs = firstArg.GetGenericArguments();

                            // parse parameters from expression
                            var      variablesSection = value.Substring(0, value.IndexOf("=>")).Trim(' ', '(', ')').Replace(" ", "");
                            string[] variables        = variablesSection.Split(',');
                            var      exprString       = value.Substring(value.IndexOf("=>") + 2).Trim();

                            int i = 0;
                            List <ParameterExpression> parameters = new List <ParameterExpression>();
                            foreach (var paramArg in paramArgs)
                            {
                                // last arg is return type
                                if (paramArg == paramArgs.Last())
                                {
                                    break;
                                }

                                parameters.Add(Expression.Parameter(paramArg, variables[i]));
                                if (variables.Length < i)
                                {
                                    i++;
                                }
                            }

                            var expr = DynamicExpressionBuilder.ParseLambda(
                                parameters.ToArray(),
                                paramArgs.Last(),
                                exprString
                                );

                            property.SetValue(result, expr, null);
                        }
                    }
                    // Handle normal enumerations
                    else if (property.PropertyType.IsEnum)
                    {
                        property.SetValue(result, Enum.Parse(property.PropertyType, value), null);
                    }
                    // string -> API.Point
                    else if (property.PropertyType == typeof(API.Point))
                    {
                        API.Point point = new API.Point()
                        {
                            X = Int32.Parse(value.Substring(0, value.IndexOf(','))),
                            Y = Int32.Parse(value.Substring(value.IndexOf(',') + 1))
                        };

                        property.SetValue(result, point, null);
                    }
                    // string -> API.Size
                    else if (property.PropertyType == typeof(API.Size))
                    {
                        API.Size size = new API.Size()
                        {
                            Width  = Int32.Parse(value.Substring(0, value.IndexOf(','))),
                            Height = Int32.Parse(value.Substring(value.IndexOf(',') + 1))
                        };

                        property.SetValue(result, size, null);
                    }
                    // Arrays
                    else if (property.PropertyType.IsArray)
                    {
                        property.SetValue(result, value, null);
                    }
                    // List<string> (deserializes as JArray)
                    else if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericArguments().First() == typeof(string) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
                    {
                        List <string> listInstance = new List <string>();
                        foreach (var item in value)
                        {
                            listInstance.Add(item.ToString());
                        }

                        property.SetValue(result, listInstance, null);
                    }
                    // List<int> (deserializes as JArray)
                    else if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericArguments().First() == typeof(Int32) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
                    {
                        List <int> listInstance = new List <int>();
                        foreach (var item in value)
                        {
                            listInstance.Add(Int32.Parse(item.ToString()));
                        }

                        property.SetValue(result, listInstance, null);
                    }
                    // List<Enum>
                    else if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericArguments().First().IsEnum&& typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
                    {
                        dynamic listInstance = Activator.CreateInstance(property.PropertyType);
                        foreach (var item in value)
                        {
                            listInstance.Add(Enum.Parse(property.PropertyType.GetGenericArguments().First(), item.ToString()));
                        }

                        property.SetValue(result, listInstance, null);
                    }
                    // Handle IConvertible types
                    else
                    {
                        property.SetValue(result, Convert.ChangeType(value, property.PropertyType), null);
                    }
                }
            }

            return(result);
        }