Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looking closely at your code, the expression you generate has some problems. See my explanation at the top of <a href="https://stackoverflow.com/questions/5744764/linq-to-sql-throwing-a-stackoverflowexception/5751931#5751931">this answer</a> 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.</p> <p>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. <code>Type -&gt; Expression&lt;Func&lt;TEntity, bool&gt;&gt;</code>. The expression you build just does not work.</p> <p>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:</p> <pre><code>// add all necessary error checking where needed and methods public class EntityPredicateDictionary { private Dictionary&lt;Type, LambdaExpression&gt; dict = new Dictionary&lt;Type, LambdaExpression&gt;(); public Expression&lt;Func&lt;TEntity, bool&gt;&gt; Predicate&lt;TEntity&gt;() where TEntity : Entity { return (Expression&lt;Func&lt;TEntity, bool&gt;&gt;)dict[typeof(TEntity)]; } public LambdaExpression Predicate(Type entityType) { return dict[entityType]; } internal void Add&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate) where TEntity : Entity { dict.Add(typeof(TEntity), predicate); } } public class LoadEntityQuery : IQuery&lt;LoadEntityQueryResult&gt; { public EntityPredicateDictionary Entities { get; private set; } public LoadEntityQuery() { Entities = new EntityPredicateDictionary(); } public void Add&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate = null) where TEntity : Entity { Entities.Add(predicate); } } // then to access the predicates LoadEntityQuery query = ...; var pred1 = query.Entities.Predicate&lt;Entity1&gt;(); var pred2 = query.Entities.Predicate(typeof(Entity2)); </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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