| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Linq.Expressions; |
| | | 5 | | |
| | | 6 | | namespace Jellyfin.Server.Implementations.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides <see cref="Expression"/> extension methods. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class ExpressionExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Combines two predicates into a single predicate using a logical OR operation. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <typeparam name="T">The predicate parameter type.</typeparam> |
| | | 17 | | /// <param name="firstPredicate">The first predicate expression to combine.</param> |
| | | 18 | | /// <param name="secondPredicate">The second predicate expression to combine.</param> |
| | | 19 | | /// <returns>A new expression representing the OR combination of the input predicates.</returns> |
| | | 20 | | public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> firstPredicate, Expression<Func<T, bool |
| | | 21 | | { |
| | 0 | 22 | | ArgumentNullException.ThrowIfNull(firstPredicate); |
| | 0 | 23 | | ArgumentNullException.ThrowIfNull(secondPredicate); |
| | | 24 | | |
| | 0 | 25 | | var invokedExpression = Expression.Invoke(secondPredicate, firstPredicate.Parameters); |
| | 0 | 26 | | return Expression.Lambda<Func<T, bool>>(Expression.OrElse(firstPredicate.Body, invokedExpression), firstPredicat |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Combines multiple predicates into a single predicate using a logical OR operation. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <typeparam name="T">The predicate parameter type.</typeparam> |
| | | 33 | | /// <param name="predicates">A collection of predicate expressions to combine.</param> |
| | | 34 | | /// <returns>A new expression representing the OR combination of all input predicates.</returns> |
| | | 35 | | public static Expression<Func<T, bool>> Or<T>(this IEnumerable<Expression<Func<T, bool>>> predicates) |
| | | 36 | | { |
| | 0 | 37 | | ArgumentNullException.ThrowIfNull(predicates); |
| | | 38 | | |
| | 0 | 39 | | return predicates.Aggregate((aggregatePredicate, nextPredicate) => aggregatePredicate.Or(nextPredicate)); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Combines two predicates into a single predicate using a logical AND operation. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <typeparam name="T">The predicate parameter type.</typeparam> |
| | | 46 | | /// <param name="firstPredicate">The first predicate expression to combine.</param> |
| | | 47 | | /// <param name="secondPredicate">The second predicate expression to combine.</param> |
| | | 48 | | /// <returns>A new expression representing the AND combination of the input predicates.</returns> |
| | | 49 | | public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> firstPredicate, Expression<Func<T, boo |
| | | 50 | | { |
| | 0 | 51 | | ArgumentNullException.ThrowIfNull(firstPredicate); |
| | 0 | 52 | | ArgumentNullException.ThrowIfNull(secondPredicate); |
| | | 53 | | |
| | 0 | 54 | | var invokedExpression = Expression.Invoke(secondPredicate, firstPredicate.Parameters); |
| | 0 | 55 | | return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(firstPredicate.Body, invokedExpression), firstPredica |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Combines multiple predicates into a single predicate using a logical AND operation. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <typeparam name="T">The predicate parameter type.</typeparam> |
| | | 62 | | /// <param name="predicates">A collection of predicate expressions to combine.</param> |
| | | 63 | | /// <returns>A new expression representing the AND combination of all input predicates.</returns> |
| | | 64 | | public static Expression<Func<T, bool>> And<T>(this IEnumerable<Expression<Func<T, bool>>> predicates) |
| | | 65 | | { |
| | 0 | 66 | | ArgumentNullException.ThrowIfNull(predicates); |
| | | 67 | | |
| | 0 | 68 | | return predicates.Aggregate((aggregatePredicate, nextPredicate) => aggregatePredicate.And(nextPredicate)); |
| | | 69 | | } |
| | | 70 | | } |