| | 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 | | { |
| 558 | 18 | | foreach (var item in specification.Items) |
| | 19 | | { |
| 228 | 20 | | if (item.Type == ItemType.Where && item.Reference is Expression<Func<T, bool>> expr) |
| | 21 | | { |
| 55 | 22 | | source = source.Where(expr); |
| | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| 51 | 26 | | return source; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <inheritdoc/> |
| | 30 | | public IEnumerable<T> Evaluate<T>(IEnumerable<T> source, Specification<T> specification) |
| | 31 | | { |
| 14 | 32 | | var compiledItems = specification.GetCompiledItems(); |
| | 33 | |
|
| 78 | 34 | | foreach (var item in compiledItems) |
| | 35 | | { |
| 25 | 36 | | if (item.Type == ItemType.Where && item.Reference is Func<T, bool> compiledExpr) |
| | 37 | | { |
| 7 | 38 | | source = source.Where(compiledExpr); |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| 14 | 42 | | return source; |
| | 43 | | } |
| | 44 | | } |