title: select anonymous delegate with linq
body: <p>I know there's a way to do this but I've been banging my head against a wall trying to figure it out. This works fine:</p>
  
  <pre><code>private GenericRecord CreateGeneric(GenericRecord g, Member m)
  {
      g.Member = m;
      return g;
  }
  
  public IList&lt;GenericRecord&gt; ReportFromDatabase(DateTime startDate, DateTime endDate)
  {
      List&lt;GenericRecord&gt; returnRecords = new List&lt;GenericRecord&gt;();
  
      returnRecords.AddRange(from r in pjRepository.Records
                             join m in memberRepository.Members on r.SID equals m.MemberId.ToString()
                             where r.TransactionDate &gt;= startDate &amp;&amp; r.TransactionDate &lt;= endDate
                             select CreateGeneric((GenericRecord)r, m));
  
      return returnRecords;
  }
  </code></pre>
  
  <p>But I know there's a way to do it without the CreateGeneric function. How do I select a delegate function inline?</p>
  
  <pre><code>returnRecords.AddRange(from r in pjRepository.Records
                         join m in memberRepository.Members on r.SID equals m.MemberId.ToString()
                         where r.TransactionDate &gt;= startDate &amp;&amp; r.TransactionDate &lt;= endDate
                         select (delegate
                         {
                             GenericRecord g = (GenericRecord)r;
                             g.Member = m;
                             return g;
                         }));
  </code></pre>
  
  <p>That gives me this exception:</p>
  
  <pre><code>The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.
  </code></pre>
  
  <p><strong>Edit:</strong> Another failed attempt</p>
  
  <pre><code>returnRecords.AddRange((from r in pjRepository.Records
                          join m in memberRepository.Members on r.SID equals m.MemberId.ToString()
                          where r.TransactionDate &gt;= startDate &amp;&amp; r.TransactionDate &lt;= endDate
                          select new { r, m }).Select(x =&gt;
                          {
                              GenericRecord g = (GenericRecord)x.r;
                              g.Member = x.m;
                              return g;
                          }));
  </code></pre>
  
  <p>This gives me:</p>
  
  <blockquote>
    <p>A lambda expression with a statement body cannot be converted to an expression tree.</p>
  </blockquote>