コード例 #1
0
        private IEnumerable <DynamicSearchModel> MatchKeysWithProperties(PropertyInfo[] properties, IEnumerable <KeyValuePair <string, string> > items)
        {
            var theItems = new Dictionary <PropertyInfo, DynamicSearchModel>();

            var quickProperties = properties.Select(x => new { Name = x.Name.ToLower(), Property = x });

            foreach (var prop in items)
            {
                var matchingProp = quickProperties.SingleOrDefault(x => x.Name == prop.Key);

                if (matchingProp == null)
                {
                    throw new Exception("Unable to find a property that matches the property name.");
                }

                var result = theItems.TryGetValue(matchingProp.Property, out var value);

                if (result)
                {
                    value.Values.Add(prop.Value);
                }
                else
                {
                    theItems.Add(matchingProp.Property, value = new DynamicSearchModel {
                        objectProperty = matchingProp.Property, Values = { prop.Value }
                    });
                }
            }

            return(theItems.Select(x => x.Value).ToList());
        }
コード例 #2
0
        public static IQueryable <T> StringSearch(IQueryable <T> query, DynamicSearchModel model)
        {
            ParameterExpression e = Expression.Parameter(typeof(T), model.objectProperty.Name);
            MemberExpression    m = Expression.MakeMemberAccess(e, model.objectProperty);

            Expression buildingExpression = m;

            if (model.objectProperty.PropertyType == typeof(string))
            {
                Expression callExpr  = Expression.Call(buildingExpression, typeof(object).GetMethod("ToString", new Type[] { }));
                Expression callExpr1 = Expression.Call(callExpr, typeof(string).GetMethod("ToLower", new Type[] { }));
                buildingExpression = Expression.Call(callExpr1, typeof(string).GetMethod("Trim", new Type[] { }));
            }

            var Constants = new List <Expression>();

            foreach (var prop in model.Values)
            {
                Expression expression = Expression.Constant(prop, prop.GetType());

                expression = Expression.Convert(expression, model.objectProperty.PropertyType);

                Constants.Add(Expression.Call(buildingExpression, model.objectProperty.PropertyType.GetMethod("Equals", new Type[] { model.objectProperty.PropertyType }), expression));
            }

            Expression chainedOrExp = null;

            foreach (var binExp in Constants)
            {
                if (chainedOrExp == null)
                {
                    chainedOrExp = binExp;
                    continue;
                }
                else
                {
                    chainedOrExp = Expression.OrElse(chainedOrExp, binExp);
                    continue;
                }
            }

            if (chainedOrExp == null)
            {
                chainedOrExp = Expression.Constant(true);
            }

            Expression <Func <T, bool> > lambda = Expression.Lambda <Func <T, bool> >(chainedOrExp, e);

            return(query.Where(lambda));
        }