title: adding where clauses to linq query
body: <p>I've found examples of how to "dynamically" add more where clauses to a linq query, but I'm not sure how to make it work with my specific query.</p>
  
  <p>Here's <a href="https://stackoverflow.com/questions/2493412/build-dynamic-linq">one example</a>, and <a href="https://stackoverflow.com/questions/6359980/proper-linq-where-clauses">here's another</a>. Now here's a look at the query that I have:</p>
  
  <pre><code>var result = (from EventLogEntry elog in el.Entries
                where (elog.InstanceId == 4107)
                orderby elog.TimeGenerated descending
                select elog).ToList();
  
  var query = from EventLogEntry elog in el.Entries
              orderby elog.TimeGenerated descending
              select elog;
  </code></pre>
  
  <p>The first example shows the hard-coded <code>InstanceID == 4107</code>, but I want to be able to add in more where clauses. All of the examples I've seen say to do:</p>
  
  <pre><code>query = query.Where(el.Entries =&gt; el.Entries.Message.Contains("error"));
  </code></pre>
  
  <p>or something like that. But my query is setting defining "the source" as an EventLogEntry object, so when I try and extend the where clause, it doesn't recognize the fields that I want to do my where on....</p>
  
  <p>Any help with this?</p>
  
  <p>This is what I tried to add, and intellisense doesn't recognize any of it:</p>
  
  <pre><code>if (!String.IsNullOrEmpty(sc.Message))
      query = query.Where(elog.Message.Contains(sc.Message));
  </code></pre>
  
  <p>Updated (this is the working version):</p>
  
  <pre><code>var query = from EventLogEntry elog in el.Entries select elog;
  
  try
  {
      if (!String.IsNullOrEmpty(sc.Message))
          query = query.Where(elog =&gt; elog.Message.Contains(sc.Message));
  
      query = query.OrderBy(elog =&gt; elog.TimeGenerated);
  
      var result = query.ToList();
  }
  catch
  {
      throw;
  }
  </code></pre>