Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: LINQ Extensions - C#

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Wed, May 04, 2022, 9:36 AM


using System;
using System.Linq.Expressions;

public static class LinqExtensions
{
    /* Adapted from
        * https://stackoverflow.com/questions/29565373/combine-listexpressionfunct-bool-to-an-or-clause-linq
        */
    public static Expression<Func<T, bool>> OrElse<T>(
        this Expression<Func<T, bool>> a,
        Expression<Func<T, bool>> b)
    {
        var bodyB = b.Body.Replace(b.Parameters[0], a.Parameters[0]);

        return Expression.Lambda<Func<T, bool>>(Expression.OrElse(a.Body, bodyB), a.Parameters);
    }


    private static Expression Replace(this Expression expression, Expression searchEx, Expression replaceEx)
    {
        return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
    }


    private class ReplaceVisitor : ExpressionVisitor
    {
        private readonly Expression _from, _to;
        public ReplaceVisitor(Expression from, Expression to)
        {
            _from = from;
            _to = to;
        }
        public override Expression Visit(Expression node)
        {
            return node == _from ? _to : base.Visit(node);
        }
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.