title: When using CSharpScript.EvaluateAsync how can I use II or && in my lambda expression
body: <p>I am working on an ASP.NET Core 2.1 API and I am trying to pass a lambda expression as a string in the body of a POST, which is then evaluated in my repository to return a set of records. I am following <a href="https://www.strathweb.com/2018/01/easy-way-to-create-a-c-lambda-expression-from-a-string-with-roslyn/" rel="nofollow noreferrer">this blog post</a> and using CSharpScript.EvaluateAsync to parse the string and generate the lambda in a form I can use in my linq query WHERE clause.</p>
  
  <p>It works great with single items. For example, if I pass </p>
  
  <pre><code>"f =&gt; f.CompanyName.Contains(\"Dav\")"
  </code></pre>
  
  <p>I get the desired record set returned.</p>
  
  <p>However, if I try...</p>
  
  <pre><code>"f =&gt; f.CompanyName.Contains(\"Dav\") || f.PrimaryContactTitle.Contains(\"Dav\")"
  </code></pre>
  
  <p>I get an exception stating... </p>
  
  <pre><code>System.NullReferenceException: Arg_NullReferenceException
  at Submission#0.&lt;&gt;c.&lt;&lt;Initialize&gt;&gt;b__0_0(Company f)
  at System.Linq.Utilities.&lt;&gt;c__DisplayClass1_0`1.&lt;CombinePredicates&gt;b__0(TSource x)
  at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray()
  at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
  at System.Linq.OrderedEnumerable`1.GetEnumerator()+MoveNext()
  at System.Linq.Enumerable.WhereEnumerableIterator`1.GetCount(Boolean onlyIfCheap)
  at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
  at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
  </code></pre>
  
  <p>when I try to work with the resultant IQueryable collection.</p>
  
  <p>Is there some alternative to using || or &amp;&amp; as OR and AND operators in a linq query lambda expression that can be correctly parsed by CSharpScript.EvaluateAsync?</p>
  
  <p>Here is an example of my request body;</p>
  
  <pre><code>{
      "filter":""f =&gt; f.CompanyName.Contains(\"Dav\") || f.PrimaryContactTitle.Contains(\"Dav\")",
      "pageNumber": 1,
      "pageSize": 20,
      "deleteFlag": false,
      "orderBy": "CompanyName desc",
      "Fields": null 
  }   
  </code></pre>
  
  <p>Here is my relevant repo code...</p>
  
  <pre><code>  public async Task&lt;PagedList&lt;Company&gt;&gt; GetFilteredPagedList(CompaniesResourceParameters resourceParams)
      {
          try
          {
  
              var options = ScriptOptions.Default.AddReferences(typeof(Company).Assembly);
              var companyFilterExpression = await CSharpScript.EvaluateAsync&lt;Func&lt;Company, bool&gt;&gt;(resourceParams.Filter, options);
  
              //Get the records ordered by the OrderBy property of the passed in resourceParams
              var collectionBeforePaging =
                  _manifestContext.Companies
                  .AsNoTracking()
                  .Include(c =&gt; c.Sites)
                  .Where(companyFilterExpression).AsQueryable()
                  .Where(d =&gt; d.DeleteFlag == false)
                  .ApplySort(resourceParams.OrderBy, _propertyMappingService.GetPropertyMapping&lt;CompanyDto, Company&gt;());
  
  
              return PagedList&lt;Company&gt;.Create(collectionBeforePaging,
                  resourceParams.PageNumber,
                  resourceParams.PageSize); // &lt;-- This is where the Exception is thrown as it tries to work with the collectionBeforePaging
          }
          catch (Exception ex)
          {
              _logger.LogError(ex, $"Exception trying to retrieve a paged list of records using the filter {resourceParams}"); 
              throw;
  
          }
      }
  </code></pre>
  
  <p>The exception occurs when I hit the line...</p>
  
  <pre><code>return PagedList&lt;Company&gt;.Create(collectionBeforePaging,
                  resourceParams.PageNumber,
                  resourceParams.PageSize)
  </code></pre>
  
  <p>Here is the Create method that is being called when the exception happens. </p>
  
  <pre><code>public static PagedList&lt;T&gt; Create(IQueryable&lt;T&gt; source, int pageNumber, int pageSize)
          {
              var count = source.Count();
              var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
              return new PagedList&lt;T&gt;(items, count, pageNumber, pageSize);
          }
  </code></pre>
  
  <p>The exception occurs when I try to use the IENumerable in the line </p>
  
  <pre><code>var count = source.Count();
  </code></pre>
  
  <p>This exception does not occur when I use a filter that does not have an OR or AND, such as..</p>
  
  <pre><code>filter":"f =&gt; f.CompanyName.Contains(\"Dav\")"
  </code></pre>
  
  <p>or </p>
  
  <pre><code>filter":"f =&gt; f.PrimaryContactName.Contains(\"Dav\")"
  </code></pre>
  
  <p>So, I believe the issue is with the use of the || operator.</p>
  
  <p><strong>* UPDATE 1 *</strong></p>
  
  <p>As NineBerry pointed out, I was not doing a null check before doing the *.Contains() in my lambda. Here is my full revised filter that I am passing into the HttpClient API request in my client. </p>
  
  <pre><code>       var filter = $"f =&gt; f.CompanyName.Contains(\"{searchValue}\") || " +
                       $"(f.PrimaryContactName != null &amp;&amp; f.PrimaryContactName.Contains(\"{searchValue}\")) || " +
                       $"(f.PrimaryContactTitle != null &amp;&amp; f.PrimaryContactTitle.Contains(\"{searchValue}\")) || " +
                       $"(f.PrimaryContactTitle != null &amp;&amp; f.PrimaryContactTitle.Contains(\"{searchValue}\")) || " +
                       $"(f.PrimaryContactEmail != null &amp;&amp; f.PrimaryContactEmail.Contains(\"{searchValue}\"))";
  </code></pre>
  
  <p>And now my search filter works as expected. </p>
  
  <p>This also verifies that I can use || and &amp;&amp; in my lambda string that is being parsed by CSharpScript.ExecuteAsync() withou any special handling. </p>