private IReadOnlyDictionary <string, string> ConvertToDictionary(SearchOptions options)
        {
            var dictionary = new Dictionary <string, string>();
            var properties = options.GetType().GetProperties();

            var stringProperties = properties
                                   .Where(propertyInfo =>
                                          propertyInfo.PropertyType == typeof(string) &&
                                          !string.IsNullOrEmpty(propertyInfo.GetValue(options) as string))
                                   .ToList();

            foreach (var stringProperty in stringProperties)
            {
                dictionary.Add(stringProperty.Name.ToLowerInvariant(), stringProperty.GetValue(options) as string);
            }

            var intProperties = properties
                                .Where(propertyInfo =>
                                       propertyInfo.PropertyType == typeof(int?) &&
                                       (propertyInfo.GetValue(options) as int?) != null)
                                .ToList();

            foreach (var intProperty in intProperties)
            {
                dictionary.Add(intProperty.Name.ToLowerInvariant(), ((int)intProperty.GetValue(options)).ToString());
            }

            return(dictionary);
        }
Exemple #2
0
        public static string ToQuery(this SearchOptions searchOptions)
        {
            var search = new StringBuilder();

            searchOptions.GetType().GetProperties().ToList().ForEach(x =>
            {
                var value = x.GetValue(searchOptions);
                if (value == null)
                {
                    return;
                }

                var att = (JsonPropertyAttribute)x.GetCustomAttributes().FirstOrDefault(z => (Type)z.TypeId == typeof(JsonPropertyAttribute));

                if (value.GetType() == typeof(List <string>))
                {
                    search.Append(string.Join("+", (value as List <string>).Select(y => $"{att.PropertyName}:\"{y}\"")));
                }
                else
                {
                    search.Append($"{att.PropertyName}:\"{value}\"");
                }

                search.Append("+AND+");
            });

            return(search.ToString().Trim("+AND+".ToCharArray()));
        }