title: generalise where clause in linq query
body: <p>I have the following linq query:</p>
  
  <pre><code>    var fileDocuments = (
          from doc in fileUploads
          from invoice in
              (
                  from inv in _dbContext.SupplierInvoiceHeaders
                  where inv.InvoiceDocumentId == doc.ID || inv.JobSheetInvoiceId == doc.ID
                  select inv
                  ).DefaultIfEmpty()
          join pos in _dbContext.PurchaseOrders on invoice.PurchaseOrder.PurchaseOrderId equals pos.PurchaseOrderId into poss
          from po in poss.DefaultIfEmpty()
          join hdf in _dbContext.HelpDeskFaults on po.HelpdeskFaultId equals hdf.ID into hdfpo
          from hs in hdfpo.DefaultIfEmpty()
          join store1 in _dbContext.Stores on hs.StoreID equals store1.ID into hsf
          from hdfStore in hsf.DefaultIfEmpty()
          join js in _dbContext.JobSheets on invoice.SupplierInvoiceHeaderId equals js.SupplierInvoiceHeaderID into jss
          from jobSheets in jss.DefaultIfEmpty()
          join ch in _dbContext.ChildProjects on po.ChildProjectId equals ch.ID into chs
          from childProjects in chs.DefaultIfEmpty()
          join ph in _dbContext.ProjectHeaders on childProjects.ProjectHeaderID equals ph.ID into phs
          from projectHeaders in phs.DefaultIfEmpty()
          join ppmsl in _dbContext.PpmScheduleLines on projectHeaders.PPMScheduleRef equals ppmsl.ID into ppsmsls
          from ppmScheduleLines in ppsmsls.DefaultIfEmpty()
          join ss2 in _dbContext.Stores on ppmScheduleLines.StoreID equals ss2.ID into ssts
          from store2 in ssts.DefaultIfEmpty()
          where getJobWhereClause(invoice, hs, ppmScheduleLines, doc) 
          select new
          {
              doc.ID,
              JobSheetId = jobSheets.DocumentID,
              doc.Name,
              doc.DateCreated,
              doc.StoreID,
              StoreName = doc.Store.Name,
              DocumentType = doc.DocumentType.Name,
              doc.DocumentTypeID
          })
          .AsEnumerable()
          .Distinct()
          .Select(d =&gt; new JobDocumentDto
          {
              ID = d.ID,
              DocumentID = (d.JobSheetId) ?? d.ID,
              DocumentName = d.Name,
              DateCreated = d.DateCreated.ToString("dd/MM/yyyy"),
              StoreName = d.StoreName,
              DocumentTypeName = d.DocumentType,
              DocumentTypeId = d.DocumentTypeID
          }).OrderByDescending(x =&gt; x.ID);
      return fileDocuments;
  </code></pre>
  
  <p>I have tried to separate the where clause into a func:</p>
  
  <pre><code>    Func&lt;SupplierInvoiceHeader, HelpDeskFault, PpmScheduleLineEntity, DocumentUploadEntity, bool&gt; getJobWhereClause = (invoice, helpDeskFault, ppmScheduleLine, doc) =&gt;
      {
          if (!string.IsNullOrEmpty(jobSearchParams.PIR) &amp;&amp; string.IsNullOrEmpty(jobSearchParams.StoreName))
          {
              return invoice.PurchaseInvoiceReference == jobSearchParams.PIR;
          }
          if (string.IsNullOrEmpty(jobSearchParams.PIR) &amp;&amp; !string.IsNullOrEmpty(jobSearchParams.StoreName))
          {
              return helpDeskFault.Store.Name.Contains(jobSearchParams.StoreName) || doc.Store.Name.Contains(jobSearchParams.StoreName) || ppmScheduleLine.Store.Name.Contains(jobSearchParams.StoreName);     
          }
  
          return invoice.PurchaseInvoiceReference == jobSearchParams.PIR &amp;&amp; (helpDeskFault.Store.Name.Contains(jobSearchParams.StoreName) || doc.Store.Name.Contains(jobSearchParams.StoreName) || ppmScheduleLine.Store.Name.Contains(jobSearchParams.StoreName));
      };
  </code></pre>
  
  <p>I get the following error message:</p>
  
  <blockquote>
    <p>Test method
    IntegrationTests.Services.DocumentUploadServiceTests.Should_Search_By_PIR
    threw exception:  System.NotSupportedException: The LINQ expression
    node type 'Invoke' is not supported in LINQ to Entities.</p>
  </blockquote>
  
  <p>Which makes sense because there is no direct translation from the func to sql but is there a way I can create an expression that will achieve what I am after?</p>