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