Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:13:25+00:00 2026-06-14T17:13:25+00:00

Using info from various SO posts and notably this blog (corrected to use AndAlso

  • 0

Using info from various SO posts and notably this blog (corrected to use AndAlso rather than And) I’ve managed to combine similarly typed linq expressions into a single predicate. But now I want to combine two Expressions where one is an input to the other. Here’s the fully expanded original Expression;

    private Expression<Func<T, bool>> ExpressionIsNamed(IEnumerable<EntityName> AccessorNames)
    {
        // works
        Expression<Func<T, bool>> Texpr = x => x.Security.Readers.Any(n => AccessorNames.ToStringArray().Contains(n.Text));

        return Texpr;
    }

Note that crucially, I need to manage these as Expressions because my DB driver needs to walk the tree and convert into a native call so using Compile() to combine is not an option.

So below is the function I want to combine with the Any() call above. The final output Expression needs to be of type Expression<Func<T, bool>> and I need to pass x.Security.Readers into this one.

    public static Expression<Func<IEnumerable<EntityName>,bool>> AccessCheckExpression(IEnumerable<EntityName> AccessorNames)
    {
        return accessList => accessList.Any(n => AccessorNames.ToStringArray().Contains(n.Text));
    }

I’ve got as far as this, but I’m struggling to work out how to swap out accessList => from accessCheck and have it use accessList in a single Expression. So far, I have this;

    private Expression<Func<T, bool>> ExpressionIsNamed(IEnumerable<EntityName> AccessorNames)
    {
        Expression<Func<T, IEnumerable<EntityName>>> accessList = (T x) => x.Security.Readers;
        Expression<Func<IEnumerable<EntityName>, bool>> accessCheck = SecurityDescriptor.AccessCheckExpression(AccessorNames);

        // Combine?
        Expression<Func<T, bool>> Texpr = ??? accessCheck + accessList ???

        return Texpr;
    }

[Update]

So I’ve got a little further;

    class ParameterUpdateVisitor : System.Linq.Expressions.ExpressionVisitor
    {
        private ParameterExpression _oldParameter;
        private ParameterExpression _newParameter;

        public ParameterUpdateVisitor(ParameterExpression oldParameter, ParameterExpression newParameter)
        {
            _oldParameter = oldParameter;
            _newParameter = newParameter;
        }

        protected override Expression VisitParameter(ParameterExpression node)
        {
            if (object.ReferenceEquals(node, _oldParameter))
                return _newParameter;

            return base.VisitParameter(node);
        }
    }

    static Expression<Func<T, bool>> UpdateParameter<T>(
        Expression<Func<T, IEnumerable<EntityName>>> expr,
        ParameterExpression newParameter)
    {
        var visitor = new ParameterUpdateVisitor(expr.Parameters[0], newParameter);
        var body = visitor.Visit(expr.Body);

        return Expression.Lambda<Func<T, bool>>(body, newParameter);
    }

I can then compile with;

UpdateParameter(accessList, accessCheck.Parameters[0]);

Thanks to all for those Invoke() suggestions, but my guess is that by the time they get to the MongoDB driver it won’t like InvocationExpression. However, both Invoke and my code above now fail in exactly the same way. Namely;

System.ArgumentException: Expression of type 
'System.Func`2[MyLib.Project,System.Collections.Generic.IEnumerable`1[MyLib.EntityName]]' 
cannot be used for parameter of type 
                            'System.Collections.Generic.IEnumerable`1[MyLib.EntityName]'

So it would appear that the implicit parameter x.Security.Readers is a different type to a plain old IEnumerable<EntityName>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:13:26+00:00Added an answer on June 14, 2026 at 5:13 pm

    VisitorExpression is your friend here. Here’s a simplified but complete example of merging something similar:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    
    class Source {
        public List<Value> Values {get;set;}
    }
    class Value {
        public int FinalValue {get;set;}
    }
    static class Program {
        static void Main() {
            Expression<Func<Source, IEnumerable<Value>>> f1 =
                source => source.Values;
            Expression<Func<IEnumerable<Value>, bool>> f2 =
                vals => vals.Any(v => v.FinalValue == 3);
    
            // change the p0 from f2 => f1
            var body = SwapVisitor.Swap(f2.Body, f2.Parameters[0], f1.Body);
            var lambda = Expression.Lambda<Func<Source, bool>>(body,f1.Parameters);
            // which is:
            // source => source.Values.Any(v => (v.FinalValue == 3))
        }
    
    }
    class SwapVisitor : ExpressionVisitor {
        private readonly Expression from, to;
        private SwapVisitor(Expression from, Expression to) {
            this.from = from;
            this.to = to;
        }
        public static Expression Swap(Expression body,
            Expression from, Expression to)
        {
            return new SwapVisitor(from, to).Visit(body);
        }
        public override Expression Visit(Expression node)
        {
             return node == from ? to : base.Visit(node);
        }
    }
    

    Edit: in your case, this would be:

    private Expression<Func<T, bool>> ExpressionIsNamed(
        IEnumerable<EntityName> AccessorNames)
    {
        Expression<Func<T, IEnumerable<EntityName>>> accessList =
            (T x) => x.Security.Readers;
        Expression<Func<IEnumerable<EntityName>, bool>> accessCheck =
            SecurityDescriptor.AccessCheckExpression(AccessorNames);
    
    
        var body = SwapVisitor.Swap(accessCheck.Body,
            accessCheck.Parameters[0], accessList.Body);
        return Expression.Lambda<Func<T, bool>>(body, accessList.Parameters);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pullout some info from an external site using jQuery and Adobe
I am trying to read some info from a text file by using windows
I am trying to extract datetime info from 2012/04/03 10:06:21:611747 using format String dateformat
I'm new with using scrapy and i'm trying to get some info from a
info: xcode 4.3.2, iOS5, using storyboard. Created project from xcode's Tabbed Application template. Did:
I am trying to decode audio samples from various file formats using ffmpeg. Therefore
I have to store some info (timestamp of post, title and link) from various
I'm trying to parse various info from log files, some of which is placed
I'm using Fancybox from FancyApps to preview content on my website. <a href=#inline class=various><span
I have been researching this problem and while there's lots of posts on various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.