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

  • SEARCH
  • Home
  • 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 8528933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:54:49+00:00 2026-06-11T08:54:49+00:00

Using the following example i would like to use my Expression inside my Contains

  • 0

Using the following example i would like to use my Expression inside my Contains method, having it pass the query onto sql server using the EF.

How can i build this up to work correctly?

void Main()
{

    IQueryable<Person> qry = GetQueryableItemsFromDB();
    var filtered = qry.Filter(p=>p.CompanyId);

}

public static class Ext
{
    public static IQueryable<T> Filter<T>(this IQueryable<T> items, Expression<Func<T, int>> resolveCompanyIdExpression)
    {      
        IEnumerable<int> validComps = GetCompanyIdsFromDataBase();        
        var exp = Expression.Lambda<Func<T, bool>>(          
            Expression.Call(typeof(Queryable),"Contains", new[] { typeof(Company) },
            Expression.Constant(validComps),
            resolveCompanyIdExpression.Body),
            resolveCompanyIdExpression.Parameters[0]);
        return items.Where(exp);  
    }  

    public static IQueryable<T> Filter<T>(this IQueryable<T> items, Expression<Func<T, IEnumerable<int>>> resolveCompanyIdExpression)
    {      
        IEnumerable<int> validComps = GetCompanyIdsFromDataBase();        
        //No Idea what to do here ?
    }  
}

public class Person
{
    public int CompanyId {get;set;}
}

I know i could pass in the entire predicate but i only want the user to supply how to resolve the Company from the entity in question.

UPDATE

I have decided to resolve the companyId rather than the entire company entity, i can get the list of ids in memory and im not fussed if that is IQueryable or just a plain array/IEnumerable

However i get some strange errors :

An exception occured during the execution of ‘
Extent.Select(o => o).Where(p => (p.Hide
= False)).Where(p => (p.Archived = False)).Where(item => System.Int32[].Contains(item.Development.CompanyId))’. See
InnerException for more details.

Inner exception is

Argument expression is not valid

UPDATE 2

I have edited the code to reflect what i would really like like to do, not having much luck on finding a solution to this.

  • 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-11T08:54:50+00:00Added an answer on June 11, 2026 at 8:54 am

    If I’m understanding correctly, what you want is expression composition:

    public static IQueryable<T> Filter<T>(IQueryable<T> query, Expression<Func<T, int>> getCompanyId) {
        IEnumerable<int> validCompanyIds = GetCompanyIdsFromDatabase();
        Expression<Func<int, bool>> filterByCompanyId = id => validCompanyIds.Contains(id);
    
        // these generics will actually be inferred, I've just written them to be explicit
        Expression<Func<T, bool>> composed = filterByCompanyId.Compose<T, int, bool>(getCompanyId);
        return query.Where(composed);
    }
    

    Below is the implementation of the Compose() extension method on expression:

        /// <summary>
        /// Composes two lambda expressions f(y) and g(x), returning a new expression representing f(g(x)).
        /// This is useful for constructing expressions to pass to functions like Where(). If given x => x.Id and id => ids.Contains(id),
        /// for example, you can create the expression x => ids.Contains(x.Id), which could be passed to Where() for an IQueryable of x's type
        /// </summary>
        /// <typeparam name="TIn">The input of g</typeparam>
        /// <typeparam name="TIntermediate">The output of g and the input of f</typeparam>
        /// <typeparam name="TOut">The output of f</typeparam>
        /// <param name="f">The outer function</param>
        /// <param name="g">The inner function</param>
        /// <returns>A new lambda expression</returns>
        public static Expression<Func<TIn, TOut>> Compose<TIn, TIntermediate, TOut>(this Expression<Func<TIntermediate, TOut>> f, Expression<Func<TIn, TIntermediate>> g)
        {
            // The implementation used here gets around EF's inability to process Invoke expressions. Rather than invoking f with the output of g, we
            // effectively "inline" g by replacing all instances of f's parameter with g's body and creating a new lambda with the rebound body of f and
            // the parameters of g
            var map = f.Parameters.ToDictionary(p => p, p => g.Body);            
            var reboundBody = ParameterRebinder.ReplaceParameters(map, f.Body);
            var lambda = Expression.Lambda<Func<TIn, TOut>>(reboundBody, g.Parameters);
            return lambda;
        }
    
    public class ParameterRebinder : ExpressionVisitor
            { 
                private readonly Dictionary<ParameterExpression, Expression> Map;
    
                public ParameterRebinder(Dictionary<ParameterExpression, Expression> map)
                {
                    this.Map = map ?? new Dictionary<ParameterExpression, Expression>();
                }
    
                public static Expression ReplaceParameters(Dictionary<ParameterExpression, Expression> map, Expression exp)
                {
                    return new ParameterRebinder(map).Visit(exp);
                } 
    
                protected override Expression VisitParameter(ParameterExpression node)
                {
                    Expression replacement;
                    if (this.Map.TryGetValue(node, out replacement))
                    {
                        return this.Visit(replacement);
                    }
                    return base.VisitParameter(node);
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following code: use LWP::Simple; my $url= http://example.com; my $html= get $url; I
I would like to know how I solve the following problem using higher order
I'm trying to implement cascading controls using the following LINQ query expression. The idea
I have the following example code and would like to know what kind of
Using the following example, I need to filter out the line containing 'ABC' only,
I'm using the following example I found on the internet to dynamically build a
I'm trying to access $a using the following example: df<-data.frame(a=c(x,x,y,y),b=c(1,2,3,4)) > df a b
I am trying to create dynamic menus from the database using the following example
I am using the GMAP3 jquery plugin. I am using the following example to
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected

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.