Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically searching using LINQ and dynamically created predicates
    primarykey
    data
    text
    <p>I have some predicates being dynamically built that have the following signature passes through as parameters into a function:</p> <pre><code>Expression&lt;Func&lt;TblTableR, bool&gt;&gt; TableRPredicate, Expression&lt;Func&lt;TblTableN, bool&gt;&gt; suspectNamesPredicate, Expression&lt;Func&lt;TblTableS, bool&gt;&gt; TableSPredicate, Expression&lt;Func&lt;TblTableI, bool&gt;&gt; suspectTableIPredicate, </code></pre> <p>I am trying to query using the following:</p> <pre><code>var registries = (from r in db.TblTableR.Where(TableRPredicate) join s in db.TblTableS.Where(TableSPredicate) on r.TblTableRID equals s.TblTableSTableRID into ss from suspects in ss.DefaultIfEmpty() join si in db.TblTableI.Where(suspectTableIPredicate) on suspects.TblTableSIndexCardID equals si.TblTableIID into sisi from suspectTableI in sisi.DefaultIfEmpty() join sn in db.TblTableN.Where(suspectNamesPredicate) on suspectTableI.TblTableIID equals sn.TblTableNIndexCardID into snsn from suspectNames in snsn.DefaultIfEmpty() select r.TblTableRID).Distinct(); </code></pre> <p>This has the result of putting any generated "where" clause into the JOIN statement eg:</p> <pre><code>left outer join tblTableI on tblTableITableRID = tblTableRID AND (expression created by predicate) </code></pre> <p>What is actually happening is that the final SQL that is created is incorrect. It is creating the following type of sql</p> <pre><code> select * from table1 left outer join table2 on field1 = field2 AND field3 = 'CRITERIA' </code></pre> <p>It is this AND clause on the end that is the problem - ending up returning too many rows. Essentially I would like to get the where clause into the statement and not have it stick the extra condition into the join. </p> <p>Something like this:</p> <pre><code>select * from table1 left outer join table2 on field1 = field2 WHERE field3 = 'CRITERIA' </code></pre> <p>I have tried adding a Where clause in as follows:</p> <pre><code>... ... ... select r.TblTableRID).Where(TableRPredicate).Distinct(); </code></pre> <p>but that will not compile because of the generic parameters on each predicate.</p> <p>If I modify my LINQ query to only select from one table and use a predicate, the WHERE clause is generated correctly.</p> <p>Any ideas?</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.
    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