CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Text.Json;
using NUnit.Framework;
//https://stackoverflow.com/questions/78432553/reduce-number-of-conditional-operators4-used-in-the-expression-maximum-allowe
//https://rules.sonarsource.com/csharp/type/Code%20Smell/RSPEC-1067/?search=expressio
public class Car
{
public int no { get; set; }
public string engine { get; set; }
public string model { get; set; }
public string color { get; set; }
public string country { get; set; }
}
#nullable enable
public static class ExpressionExtensions
{
public static Expression<Func<T,bool>> AndAlsoAll<T>(this Expression<Func<T,bool>> expression, params Expression<Func<T,bool>> [] others)
{
var body = expression.Body;
var parameter = expression.Parameters[0];
foreach (var other in others)
body = Expression.AndAlso(body, new ParameterReplacer((other.Parameters[0], (Expression)parameter)).Visit(other.Body));
return Expression.Lambda<Func<T, bool>>(body, parameter);
}
}
class ParameterReplacer : ExpressionVisitor
{
// Replace formal parameters (e.g. of a lambda body) with some containing expression in scope.
readonly Dictionary<ParameterExpression, Expression> parametersToReplace;
public ParameterReplacer(params (ParameterExpression parameter, Expression replacement) [] parametersToReplace) =>
this.parametersToReplace = parametersToReplace.ToDictionary(p => p.parameter, p => p.replacement);
protected override Expression VisitParameter(ParameterExpression p) =>
parametersToReplace.TryGetValue(p, out var e) ? e : base.VisitParameter(p);
}
class MockupRepository<T>
{
IQueryable<T> db { get; }
public MockupRepository(IQueryable<T> db) => this.db = db;
public Task<IEnumerable<T>> ReadDocumentsWhereAll(Expression<Func<T,bool>> predicate, params Expression<Func<T,bool>> [] andAlsoAllPredicates)
{
return ReadDocuments(predicate.AndAlsoAll(andAlsoAllPredicates));
}
public Task<IEnumerable<T>> ReadDocuments(Expression<Func<T,bool>> predicate)
{
IEnumerable<T> result = db.Where(predicate);
return Task.FromResult(result);
}
}
class TestClass
{
MockupRepository<Car> carRepository = new (
new List<Car>()
{
new() { no = 101, engine = "gas", model = "studebaker", color = "mauve", country = "foo" },
new() { no = 201, engine = "gas", model = "DeLorean", color = "stainless steel", country = "foo" },
}.AsQueryable());
public async Task TestQuery()
{
int no = 101;
var engine = "gas";
var model = "studebaker";
var color = "mauve";
var country = "foo";
var cars1 = await this.carRepository.ReadDocuments(car => car.no == no &&
car.engine == engine && car.model == model && car.color == color && car.country==country);
cars1.ToList().Dump();
var cars = await this.carRepository.ReadDocumentsWhereAll(car => car.no == no,
car => car.engine == engine,
car => car.model == model,
car => car.color == color,
car => car.country==country);
Assert.That(JsonSerializer.Serialize(cars1) == JsonSerializer.Serialize(cars));
}
public void TestAndAlsoAll()
{
int no = 101;
var engine = "gas";
var model = "studebaker";
var color = "mauve";
var country = "foo";
Expression<Func<Car,bool>> original =
car => car.no == no && car.engine == engine && car.model == model && car.color == color && car.country==country;
var combined = ExpressionExtensions.AndAlsoAll<Car>(
(car => car.no == no),
x => x.engine == engine, car => car.model == model, car => car.color == color, car => car.country==country);
Console.WriteLine(combined);
Assert.That(combined.ToString() == original.ToString());
}
}
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Environment version: {0} ({1}), {2}.\n", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
new TestClass().TestAndAlsoAll();
Console.WriteLine();
await new TestClass().TestQuery();
}
}