Proper usage of Select() and Where() in a single statement
<p>I am trying to write a query in the controller similar to what I have in my SQL stored procedure:</p>

<pre><code>Select * FROM TableName Where aDate &gt;= beginDate and aDate &lt;= endDate
</code></pre>

<p>In my controller, I have declared 2 variables, which will be getting input values from my view file:</p>

<pre><code>DateTime? startDate = input.BeginDateInput;
DateTime? endDate = input.EndDateInput;
</code></pre>

<p>and variable q that takes values from db</p>

<pre><code>var q = db.tblName.AsQueryable();
</code></pre>

<p>Currently, I have a Select-based table of all values, looking like the following:</p>

<pre><code>var data = q.Select(
  p =&gt;
   new
   {
       //...
       WarrantyExpDate = p.WarrantyExpDate.ToShortDateString(),
       //...
   });
</code></pre>

<p>When I was trying to add .Where() at the end of </p>

<pre><code>Select(...).Where(DateTime.Compare(Convert.ToDateTime(WarrantyExpDate), (DateTime)startDate)&gt;0)
</code></pre>

<p>(shorten for time sake), I was receiving an error <code>WarrantyExpDate was not in the current context</code>. After performing some research, I tried to recreate the suggestion from <a href="https://stackoverflow.com/questions/9410321/c-sharp-using-select-and-where-in-a-single-linq-statement">that thread</a> to swap select and where, but it was returning the same issue.</p>

<p>Can anyone please point me in the right direction on what I am doing wrong? How do I made both Select and Where recognize the same variable?</p>