Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to break apart the Linq expression inside an Any clause
    primarykey
    data
    text
    <p>I am trying to make a modular Linq query (to an OData source).</p> <p>This is a simplified version of my query:</p> <pre><code>// Any clause that I want to be modular Func&lt;Encounter, bool&gt; orderAnyClause = x =&gt; x.OrderName.StartsWith("Order 00"); // Query using the any clause var result = entities.Customers.Where(cust=&gt; cust.Orders.Any(orderAnyClause)); // A method to do the selection. It works just fine. IQueryable&lt;SearchSelectionResult&gt; selectedResults = SelectResults(result); // This throws the exception shown below var list = selectedResults.ToList(); </code></pre> <p>This all compiles fine, but when I run it my any clause causes this exception:</p> <blockquote> <p>Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.LambdaExpression'.</p> </blockquote> <p>I know it is my any clause because if I embed the the clause in the statement, it all works fine.</p> <p><strong>Why am I getting this error? And how can I break this statement out and not get the error?</strong></p> <hr> <p><strong>Update: Using an Expression</strong></p> <p>I tried to use an expression like this:</p> <pre><code>Expression&lt;Func&lt;Encounter, bool&gt;&gt; orderAnyClause = x =&gt; x.OrderName.StartsWith("Order 00"); </code></pre> <p>And I get the following compile time error message:</p> <pre><code>Instance argument: cannot convert from System.Data.Services.Client.DataServiceCollection&lt;ODataComponetizedQueriesTest.MyEntities.Order&gt;' to 'System.Linq.IQueryable&lt;ODataComponetizedQueriesTest.MyEntities.Order&gt;' </code></pre>
    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.
    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