private static Expression<Func<T, bool>> GetComparisonExpression<T>(Expression<Func<T, bool>> predicate, SearchCriteria criteria, SearchOperation operation) where T : ISearchResult { var boost = criteria.Boost; var value = criteria.Value; switch (value) { case DateTime _: var compareDateTime = (DateTime)value; predicate = (criteria.Invert || criteria.Filter == FilterType.LessThan) ? predicate.AddPredicate(i => ((DateTime)i[(ObjectIndexerKey)criteria.Field] < compareDateTime).Boost(boost), operation) : predicate.AddPredicate(i => ((DateTime)i[(ObjectIndexerKey)criteria.Field] > compareDateTime).Boost(boost), operation); break; case double _: var compareDouble = (double)value; predicate = (criteria.Invert || criteria.Filter == FilterType.LessThan) ? predicate.AddPredicate(i => ((double)i[(ObjectIndexerKey)criteria.Field] < compareDouble).Boost(boost), operation) : predicate.AddPredicate(i => ((double)i[(ObjectIndexerKey)criteria.Field] > compareDouble).Boost(boost), operation); break; case int _: var compareInt = (int)value; predicate = (criteria.Invert || criteria.Filter == FilterType.LessThan) ? predicate.AddPredicate(i => ((int)i[(ObjectIndexerKey)criteria.Field] < compareInt).Boost(boost), operation) : predicate.AddPredicate(i => ((int)i[(ObjectIndexerKey)criteria.Field] > compareInt).Boost(boost), operation); break; } return predicate; }
private static Expression<Func<T, bool>> GetRangeExpression<T>(Expression<Func<T, bool>> predicate, SearchCriteria criteria, SearchOperation operation) where T : ISearchResult { var inclusion = (criteria.Filter == FilterType.InclusiveRange) ? Inclusion.Both : Inclusion.None; var boost = criteria.Boost; var value = criteria.Value; if (value is object[]) { switch (value) { case string[] s: value = s; break; case DateTime[] datetime: value = datetime; break; case double[] d: value = d; break; case int[] i: value = i; break; } } switch (value) { case string[] _: var pairString = (string[])value; var leftString = pairString[0]; var rightString = pairString[1]; predicate = criteria.Invert ? predicate.AddPredicate(i => !i[criteria.Field].Between(leftString, rightString, inclusion).Boost(boost), operation) : predicate.AddPredicate(i => i[criteria.Field].Between(leftString, rightString, inclusion).Boost(boost), operation); break; case DateTime[] _: var pairDateTime = (DateTime[])value; var leftDateTime = pairDateTime[0]; var rightDateTime = pairDateTime[1]; predicate = criteria.Invert ? predicate.AddPredicate(i => !((DateTime)i[(ObjectIndexerKey)criteria.Field]).Between(leftDateTime, rightDateTime, inclusion).Boost(boost), operation) : predicate.AddPredicate(i => ((DateTime)i[(ObjectIndexerKey)criteria.Field]).Between(leftDateTime, rightDateTime, inclusion).Boost(boost), operation); break; case double[] _: var pairDouble = (double[])value; var leftDouble = pairDouble[0]; var rightDouble = pairDouble[1]; predicate = criteria.Invert ? predicate.AddPredicate(i => !((double)i[(ObjectIndexerKey)criteria.Field]).Between(leftDouble, rightDouble, inclusion).Boost(boost), operation) : predicate.AddPredicate(i => ((double)i[(ObjectIndexerKey)criteria.Field]).Between(leftDouble, rightDouble, inclusion).Boost(boost), operation); break; case int[] _: var pairInt = (int[])value; var leftInt = pairInt[0]; var rightInt = pairInt[1]; predicate = criteria.Invert ? predicate.AddPredicate(i => !((int)i[(ObjectIndexerKey)criteria.Field]).Between(leftInt, rightInt, inclusion).Boost(boost), operation) : predicate.AddPredicate(i => ((int)i[(ObjectIndexerKey)criteria.Field]).Between(leftInt, rightInt, inclusion).Boost(boost), operation); break; } return predicate; }