Files
jellyfin/Jellyfin.Server.Implementations/Extensions/ExpressionExtensions.cs
Tim Eisele 3fc3b04daf
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
OpenAPI / OpenAPI - HEAD (push) Waiting to run
OpenAPI / OpenAPI - BASE (push) Waiting to run
OpenAPI / OpenAPI - Difference (push) Blocked by required conditions
OpenAPI / OpenAPI - Publish Unstable Spec (push) Blocked by required conditions
OpenAPI / OpenAPI - Publish Stable Spec (push) Blocked by required conditions
Tests / run-tests (macos-latest) (push) Waiting to run
Tests / run-tests (ubuntu-latest) (push) Waiting to run
Tests / run-tests (windows-latest) (push) Waiting to run
Project Automation / Project board (push) Waiting to run
Merge Conflict Labeler / Labeling (push) Waiting to run
Rework parental ratings (#12615)
2025-03-30 21:51:54 -06:00

71 lines
3.5 KiB
C#

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));
}
}