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 5842405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:00:49+00:00 2026-05-22T12:00:49+00:00

I am trying build a dictionary of Expressions that have different input parameter types.

  • 0

I am trying build a dictionary of Expressions that have different input parameter types. I am trying to store the type of the parameter because later down the road I plan to use Reflection to discover a method on the type. Here is the code that creates the dictionary and a generic Add function I created to add entries to it:

public class LoadEntityQuery : IQuery<LoadEntityQueryResult>
{
    public IDictionary<Type, Expression<Func<Type, bool>>> Entities { get; set; } 
    public LoadEntityQuery()
    {
        Entities = new Dictionary<Type, Expression<Func<Type, bool>>>();
    }

    public void Add<T>(Expression<Func<T, bool>> where = null) where T : Entity
    {
        Expression<Func<Type, bool>> _lambda = null;

        if (where != null)
        {
            ParameterExpression param = Expression.Parameter(typeof(T), where.Parameters[0].Name);

            var body = Expression.Invoke(where, param);
            _lambda = Expression.Lambda<Func<Type, bool>>(body, param);
        }

        Entities.Add(typeof(T), _lambda);
    }
}

The body of the new method is created properly. The issue is when I try to create the new Lambda expression with the type from the expression being passed in, I receive this error:

ParameterExpression of type ‘TestNamespace.TestClass’ cannot be used for delegate parameter of type ‘System.Type’

Does anybody have an idea as to what I can do in this situation? Like I said before, at some point later I am going to loop through this dictionary to do some reflective programming on each entry. If there is a better way to do this I am all ears.

As an example of what I am trying to do, I store the expressions for Where clauses for POCO objects that need to be initialized:

LoadEntityQuery _query = new LoadEntityQuery();
    _query.Add<PayrollLocation>();
    _query.Add<PayrollGroupBU>();
    _query.Add<PersonnelPosition>(t => t.DataSet == MasterDataSet);
    _query.Add<EmployeeStatus>();
    _query.Add<PayrollGrade>();

This list of Entities will be different for each app. The idea is to collect all the entities and Where clause for each and discover a certain method using reflection on each one. (e.g. PayrollLocation has a GetPayrollLocationsQuery() method, PayrollGroupBU has a GetPayrollGroupBUQuery() method…). The Add method is generic in order for me to make use of the lambda expression in the calling code.

Thanks,
Jason

  • 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-05-22T12:00:50+00:00Added an answer on May 22, 2026 at 12:00 pm

    Looking closely at your code, the expression you generate has some problems. See my explanation at the top of this answer to explain one of them, it’s the same issue here. You’re creating a new lambda where the parameter instance you create here is not used in the body.

    The bigger problem is that your expressions are just wrong for what you appear to be trying to do. As far as I can tell, you are just trying to create a mapping from entity types to functions that take an entity of that type and returns a bool. Type -> Expression<Func<TEntity, bool>>. The expression you build just does not work.

    You should make the dictionary store non-generic lambdas that way you can store these functions easily without performing conversions or rebuilding the expressions. You will not be able to store them as generic lambdas here. Then cast to the generic lambda when you access them. I’d put this in a separate class to manage the casting and refactor your code to this:

    // add all necessary error checking where needed and methods
    public class EntityPredicateDictionary
    {
        private Dictionary<Type, LambdaExpression> dict = new Dictionary<Type, LambdaExpression>();
    
        public Expression<Func<TEntity, bool>> Predicate<TEntity>() where TEntity : Entity
        {
            return (Expression<Func<TEntity, bool>>)dict[typeof(TEntity)];
        }
    
        public LambdaExpression Predicate(Type entityType)
        {
            return dict[entityType];
        }
    
        internal void Add<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : Entity
        {
            dict.Add(typeof(TEntity), predicate);
        }
    }
    
    public class LoadEntityQuery : IQuery<LoadEntityQueryResult>
    {
        public EntityPredicateDictionary Entities { get; private set; }
        public LoadEntityQuery()
        {
            Entities = new EntityPredicateDictionary();
        }
    
        public void Add<TEntity>(Expression<Func<TEntity, bool>> predicate = null) where TEntity : Entity
        {
            Entities.Add(predicate);
        }
    }
    
    // then to access the predicates
    LoadEntityQuery query = ...;
    var pred1 = query.Entities.Predicate<Entity1>();
    var pred2 = query.Entities.Predicate(typeof(Entity2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an array that contains arrays of dictionary objects in
I'm trying to build template tags that take use a dictionary in settings.py to
As an exercise, I have been trying to build a non-GUI boggle type game
I am trying to build a pie chart from a dictionary. Before I display
I have 2 databases that have the same structure, but different data. Both are
I'm trying to build words in arrays by their key value in a dictionary.
I'm trying to build my own inversion of control container. Right now I store
I am trying to build a dictionary with two keys but am getting a
I am trying to build an autocomplete script. The dictionary is on a separate
I found a dictionary of Chinese characters in unicode. I'm trying to build a

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.