Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the <code>let</code> syntax in your code:</p> <pre><code>string test = "12,23,34,23,12"; var res = from string s in test.Split(',') let u = s+s select int.Parse(u); </code></pre> <p>Alternately, you could use the LINQ extension methods directly instead of the special syntax:</p> <pre><code>string test = "12,23,34,23,12"; var res = test.Split(',') .Select(s =&gt; { var u = s + s; return int.Parse(u); }); </code></pre> <p><strong>Since the question was updated:</strong></p> <p>I don't mean to be disrespectful, but I think this solution isn't necessary.</p> <p>Here's a bit of an exploration: </p> <p>If we want to accept truly "arbitrary" lambdas like you say, then they can come from an outside source, and <code>Wrap</code> does nothing because it's the same as <code>f()</code>:</p> <pre><code>// with 'f' as some arbitrary lambda, then this: var res = from string s in test.Split(',') select f.Wrap(); // is the same as: var res = from string s in test.Split(',') select f(); </code></pre> <p>But if you do this, <code>f</code> can't depend upon <code>s</code> in any way (for example, this way you can't write your example code):</p> <pre><code>// e.g. where does s come from? var f = () =&gt; { var u = s+s; return int.Parse(u); }; // we can write it like this, as a function of 's': var f2 = s =&gt; { var u = s+s; return int.Parse(u); }; //but then you can just use "select f2(s)" and we're back not needing Wrap any more </code></pre> <p>What we're really looking for is arbitrary closures over <code>s</code>. For this to happen, the lambdas have to be defined inline, where <code>s</code> is in scope, so you aren't really accepting truly "arbitrary" lambdas any more, and they have to be written directly in the code.</p> <p>This is why I proposed the <code>let</code> syntax, since any lambda you can come up with can be converted to that syntax, and it goes with the rest of the query syntax. This is what <code>let</code> is designed for! :)</p> <p>Alternately, you could just use lambdas which take parameters like <code>f2</code> above.</p> <p>If you really want to stick with the lambda syntax, I'd suggest using the extension methods. Like I said in my comment, it looks like you're looking for something halfway between query &amp; extension syntax.</p> <p>I'd be interested in why you want to use the lambda syntax with the query syntax? </p> <p>Hope this helps :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload