Note that there are some explanatory texts on larger screens.

plurals
  1. POResolving LINQ Parameters that were passed to method in which LINQ Expression is
    text
    copied!<p>I am working on LINQ to SQL translator. It should translate LINQ queries to SQL. I am focused on creating WHERE part of the query. I am traversing LINQ Expression tree and then I get to a problem that I can't get to the value of actual parameter passed to the method that contains LINQ call.</p> <p>NOTE: I am not using LINQ to SQL, nor Entity Framework, nor NHibernate because I simply can't since VB6 Legacy system is in the game.</p> <p>So what I am trying to achieve is to call somewhere on high level this:</p> <pre><code>int? parameterForCall = cmb.SelectedValue; </code></pre> <p>Then I have a method like this and it is in <code>ExpenseDAL</code> class that calls <code>BaseDAL&lt;Expense&gt;.GetAll(X)</code>:</p> <pre><code>public IList&lt;Expense&gt; GetAll(int? parameterForCall) { IList&lt;Expense&gt; expenses = BaseDAL&lt;Expense&gt;.GetAll(t =&gt; t.Fixed == parameterForCall); } </code></pre> <p><code>GetAll</code> method has signature like this:</p> <pre><code>public static IList&lt;T&gt; GetAll(Expression&lt;Func&lt;T, bool&gt;&gt; predicate = null); </code></pre> <p>Then I am calling <code>GetCondition</code> method that converts expression to SQL:</p> <pre><code>private static string GetCondition(Expression predicate = null); </code></pre> <p>It is a recursive function. In it I get to a situation that I need to get to parameter that I passed to <code>GetAll</code> expression, named <code>parameterForCall</code>.</p> <p>The problem is that I can write this:</p> <pre><code>dynamic value = (predicate as ConstantExpression); </code></pre> <p>And in ImmediateWindow I can see that value.Value is written like this:</p> <pre><code>{FMC.Proxy.Common.BaseDAL.} parameterForCall: 19 </code></pre> <p>But I can't get to the value 19. And I want it so I can convert it to value to put to SQL string.</p> <p>Does anyone know how to get to value 19 so I can use it in the code?</p>
 

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