Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert Expression<Func<T, bool>> to Expression<Func<Type, bool>>?
    primarykey
    data
    text
    <p>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:</p> <pre><code>public class LoadEntityQuery : IQuery&lt;LoadEntityQueryResult&gt; { public IDictionary&lt;Type, Expression&lt;Func&lt;Type, bool&gt;&gt;&gt; Entities { get; set; } public LoadEntityQuery() { Entities = new Dictionary&lt;Type, Expression&lt;Func&lt;Type, bool&gt;&gt;&gt;(); } public void Add&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; where = null) where T : Entity { Expression&lt;Func&lt;Type, bool&gt;&gt; _lambda = null; if (where != null) { ParameterExpression param = Expression.Parameter(typeof(T), where.Parameters[0].Name); var body = Expression.Invoke(where, param); _lambda = Expression.Lambda&lt;Func&lt;Type, bool&gt;&gt;(body, param); } Entities.Add(typeof(T), _lambda); } } </code></pre> <p>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:</p> <p>ParameterExpression of type 'TestNamespace.TestClass' cannot be used for delegate parameter of type 'System.Type'</p> <p>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.</p> <p>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:</p> <pre><code>LoadEntityQuery _query = new LoadEntityQuery(); _query.Add&lt;PayrollLocation&gt;(); _query.Add&lt;PayrollGroupBU&gt;(); _query.Add&lt;PersonnelPosition&gt;(t =&gt; t.DataSet == MasterDataSet); _query.Add&lt;EmployeeStatus&gt;(); _query.Add&lt;PayrollGrade&gt;(); </code></pre> <p>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.</p> <p>Thanks, Jason</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload