aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Extensions/ExpressionExtensions.cs
blob: d70ac672f2bc29c8981987721849c83c2925f5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Jellyfin.Server.Implementations.Extensions;

/// <summary>
/// Provides <see cref="Expression"/> extension methods.
/// </summary>
public static class ExpressionExtensions
{
    /// <summary>
    /// Combines two predicates into a single predicate using a logical OR operation.
    /// </summary>
    /// <typeparam name="T">The predicate parameter type.</typeparam>
    /// <param name="firstPredicate">The first predicate expression to combine.</param>
    /// <param name="secondPredicate">The second predicate expression to combine.</param>
    /// <returns>A new expression representing the OR combination of the input predicates.</returns>
    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> firstPredicate, Expression<Func<T, bool>> secondPredicate)
    {
        ArgumentNullException.ThrowIfNull(firstPredicate);
        ArgumentNullException.ThrowIfNull(secondPredicate);

        var invokedExpression = Expression.Invoke(secondPredicate, firstPredicate.Parameters);
        return Expression.Lambda<Func<T, bool>>(Expression.OrElse(firstPredicate.Body, invokedExpression), firstPredicate.Parameters);
    }

    /// <summary>
    /// Combines multiple predicates into a single predicate using a logical OR operation.
    /// </summary>
    /// <typeparam name="T">The predicate parameter type.</typeparam>
    /// <param name="predicates">A collection of predicate expressions to combine.</param>
    /// <returns>A new expression representing the OR combination of all input predicates.</returns>
    public static Expression<Func<T, bool>> Or<T>(this IEnumerable<Expression<Func<T, bool>>> predicates)
    {
        ArgumentNullException.ThrowIfNull(predicates);

        return predicates.Aggregate((aggregatePredicate, nextPredicate) => aggregatePredicate.Or(nextPredicate));
    }

    /// <summary>
    /// Combines two predicates into a single predicate using a logical AND operation.
    /// </summary>
    /// <typeparam name="T">The predicate parameter type.</typeparam>
    /// <param name="firstPredicate">The first predicate expression to combine.</param>
    /// <param name="secondPredicate">The second predicate expression to combine.</param>
    /// <returns>A new expression representing the AND combination of the input predicates.</returns>
    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> firstPredicate, Expression<Func<T, bool>> secondPredicate)
    {
        ArgumentNullException.ThrowIfNull(firstPredicate);
        ArgumentNullException.ThrowIfNull(secondPredicate);

        var invokedExpression = Expression.Invoke(secondPredicate, firstPredicate.Parameters);
        return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(firstPredicate.Body, invokedExpression), firstPredicate.Parameters);
    }

    /// <summary>
    /// Combines multiple predicates into a single predicate using a logical AND operation.
    /// </summary>
    /// <typeparam name="T">The predicate parameter type.</typeparam>
    /// <param name="predicates">A collection of predicate expressions to combine.</param>
    /// <returns>A new expression representing the AND combination of all input predicates.</returns>
    public static Expression<Func<T, bool>> And<T>(this IEnumerable<Expression<Func<T, bool>>> predicates)
    {
        ArgumentNullException.ThrowIfNull(predicates);

        return predicates.Aggregate((aggregatePredicate, nextPredicate) => aggregatePredicate.And(nextPredicate));
    }
}