Note that there are some explanatory texts on larger screens.

plurals
  1. POIn LINQ how to use column value passed to IQueryable function with func<T,T2>
    primarykey
    data
    text
    <p>Let's say I have this function to <strong>add a simple filter</strong> to my query:</p> <pre><code> public static IQueryable&lt;MyType&gt; FilterSpecificParam(this IQueryable&lt;MyType&gt; query, string param) { var req = query.Where(c =&gt; (!(param == null || param.Trim() == "") &amp;&amp; c.SpecificColumn.StartsWith(param)) || ((param == null || param.Trim() == ""))); return req; } </code></pre> <p>I would use it like this:</p> <pre><code>SomeQueryOnMyTable.FilterSpecificParam(MyFilter) </code></pre> <p>Now, I don't want to repeat such a function for every filter I want to set! So, I figured out that I could write my query like this :</p> <pre><code>SomeQueryOnMyTable.FilterGenericParam(MyFilter, c =&gt; c.TheColumnIWantToTest)) </code></pre> <p>having this prototype for my function:</p> <pre><code> public static IQueryable&lt;MyType&gt; Filter(this IQueryable&lt;MyType&gt; query, string param, Func&lt;MyType, string&gt; predicate) </code></pre> <p><strong>The problem:</strong> I don't manage to get a hand on the value of the column:</p> <pre><code> public static IQueryable&lt;MyType&gt; Filter(this IQueryable&lt;MyType&gt; query, string param, Func&lt;MyType, string&gt; predicate) { var req = query.Where(c =&gt; (!(param == null || param.Trim() == "") &amp;&amp; predicate.Invoke(What???).StartsWith(param)) || ((param == null || param.Trim() == ""))); return req; } </code></pre> <p>Notice the <strong>predicate.Invoke</strong> in the code above: it asks for an argument of type <em>MyType</em>.</p> <p>I'd like it to take the argument of the preceding query (SomeQueryOnMyTable), but I don't have a clue of how to do this... Any idea?</p> <p><strong>EDIT:</strong> Maybe there is a better way to do this generic filter function, I will eventually accept it as an answer</p>
    singulars
    1. This table or related slice is empty.
    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. 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