title: How to insert a lambda into a Linq declarative query expression
body: <p>Let's say you have the following code:</p>
  
  <pre><code>  string encoded="9,8,5,4,9";
  
    // Parse the encoded string into a collection of numbers
    var nums=from string s in encoded.Split(',')
             select int.Parse(s);
  </code></pre>
  
  <p>That's easy, but what if I want to apply a lambda expression to s in the select, but still keep this as a declarative query expression, in other words:</p>
  
  <pre><code>  string encoded="9,8,5,4,9";
  
    // Parse the encoded string into a collection of numbers
    var nums=from string s in encoded.Split(',')
             select (s =&gt; {/* do something more complex with s and return an int */});
  </code></pre>
  
  <p>This of course does not compile. But, how can I get a lambda in there without switching this to fluent syntax.</p>
  
  <p><strong>Update</strong>: Thanks to guidance from StriplingWarrior, I have a convoluted but compilable solution:</p>
  
  <pre><code>var result=from string s in test.Split(',')
             select ((Func&lt;int&gt;) 
               (() =&gt; {string u="1"+s+"2"; return int.Parse(u);}))();
  </code></pre>
  
  <p>The key is in the cast to a <code>Func&lt;string,int&gt;</code> followed by evaluation of the lambda for each iteration of the select with <code>(s)</code>. Can anyone come up with anything simpler (i.e., without the cast to Func followed by its evaluation or perhaps something less verbose that achieves the same end result while maintaining the query expression syntax)? </p>
  
  <p>Note: The lambda content above is trivial and exemplary in nature. Please don't change it.</p>
  
  <p><strong>Update 2</strong>: Yes, it's me, crazy Mike, back with an alternate (prettier?) solution to this:</p>
  
  <pre><code>public static class Lambda
  {
    public static U Wrap&lt;U&gt;(Func&lt;U&gt; f)
    {
      return f();
    }
  }
  ... 
    // Then in some function, in some class, in a galaxy far far away:
  
    // Look what we can do with no casts
    var res=from string s in test.Split(',')
            select Lambda.Wrap(() =&gt; {string u="1"+s+"2"; return int.Parse(u);});
  </code></pre>
  
  <p>I think this solves the problem without the ugly cast and parenarrhea. Is something like the Lambda.Wrap generic method already present somewhere in the .NET 4.0 Framework, so that I do not have to reinvent the wheel? Not to overburden this discussion, I have moved this point into its own question: <a href="https://stackoverflow.com/questions/3973799/does-the-following-generic-method-already-exist-in-the-net-4-0-framework">Does this "Wrap" generic method exist in .NET 4.0</a>.</p>