| | | 1 | | namespace Pozitron.QuerySpecification; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents an evaluator for where expressions. |
| | | 5 | | /// </summary> |
| | | 6 | | [EvaluatorDiscovery(Order = 10)] |
| | | 7 | | public sealed class WhereEvaluator : IEvaluator, IMemoryEvaluator |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gets the singleton instance of the <see cref="WhereEvaluator"/> class. |
| | | 11 | | /// </summary> |
| | 3 | 12 | | public static WhereEvaluator Instance = new(); |
| | 6 | 13 | | private WhereEvaluator() { } |
| | | 14 | | |
| | | 15 | | /// <inheritdoc/> |
| | | 16 | | public IQueryable<T> Evaluate<T>(IQueryable<T> source, Specification<T> specification) where T : class |
| | | 17 | | { |
| | 572 | 18 | | foreach (var item in specification.Items) |
| | | 19 | | { |
| | 234 | 20 | | if (item.Type == ItemType.Where && item.Reference is Expression<Func<T, bool>> expr) |
| | | 21 | | { |
| | 57 | 22 | | source = source.Where(expr); |
| | | 23 | | } |
| | | 24 | | } |
| | | 25 | | |
| | 52 | 26 | | return source; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc/> |
| | | 30 | | public IEnumerable<T> Evaluate<T>(IEnumerable<T> source, Specification<T> specification) |
| | | 31 | | { |
| | 15 | 32 | | var compiledItems = specification.GetCompiledItems(); |
| | | 33 | | |
| | 84 | 34 | | foreach (var item in compiledItems) |
| | | 35 | | { |
| | 27 | 36 | | if (item.Type == ItemType.Where && item.Reference is Func<T, bool> compiledExpr) |
| | | 37 | | { |
| | 9 | 38 | | source = source.Where(compiledExpr); |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | 15 | 42 | | return source; |
| | | 43 | | } |
| | | 44 | | } |