Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3242958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:22:36+00:00 2026-05-17T18:22:36+00:00

Does the following generic function exist anywhere in the .NET 4.0 framework? I would

  • 0

Does the following generic function exist anywhere in the .NET 4.0 framework? I would like to reuse it if it does rather than writing it myself:

public static class Lambda 
{ 
  public static U Wrap<U>(Func<U> f) 
  { 
    return f(); 
  } 
} 

It allows for the following construct (i.e., lambda expressions embedded in the select clause of a LINQ query):

string test="12,23,34,23,12";
var res=from string s in test.Split(',') 
        select Lambda.Wrap(() => {string u=s+s; return int.Parse(u);});

Update: To all people questioning this solution, look at Onkelborg’s answer to see what it would take to include the lambda without Lambda.Wrap() (while still maintaining query syntax). Please do not eliminate the lambda. This has to work for abitrary (value returning) lambdas. Also, I am looking for a query syntax solution, only. Please do not convert the expression to fluent syntax, thus trivializing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:22:36+00:00Added an answer on May 17, 2026 at 6:22 pm

    You can use the let syntax in your code:

    string test = "12,23,34,23,12";
    var res = from string s in test.Split(',') 
              let u = s+s
              select int.Parse(u);
    

    Alternately, you could use the LINQ extension methods directly instead of the special syntax:

    string test = "12,23,34,23,12";
    var res = test.Split(',')
                  .Select(s => { var u = s + s; return int.Parse(u); });
    

    Since the question was updated:

    I don’t mean to be disrespectful, but I think this solution isn’t necessary.

    Here’s a bit of an exploration:

    If we want to accept truly “arbitrary” lambdas like you say, then they can come from an outside source, and Wrap does nothing because it’s the same as f():

    // 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();
    

    But if you do this, f can’t depend upon s in any way (for example, this way you can’t write your example code):

    // e.g. where does s come from?
    var f = () => { var u = s+s; return int.Parse(u); }; 
    
    // we can write it like this, as a function of 's':
    var f2 = s => { 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
    

    What we’re really looking for is arbitrary closures over s. For this to happen, the lambdas have to be defined inline, where s is in scope, so you aren’t really accepting truly “arbitrary” lambdas any more, and they have to be written directly in the code.

    This is why I proposed the let 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 let is designed for! 🙂

    Alternately, you could just use lambdas which take parameters like f2 above.

    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 & extension syntax.

    I’d be interested in why you want to use the lambda syntax with the query syntax?

    Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does the following mean? Class.Function(variable := 1 + 1) What is this operator
As per my understanding the following generic function in java: public static <T> T
Why does the following code sometimes causes an Exception with the contents CLIPBRD_E_CANT_OPEN: Clipboard.SetText(str);
Why does the following code not work as I was expecting? <?php $data =
Why does the following method hang? public void pipe(Reader in, Writer out) { CharBuffer
What does the following code do in C/C++? if (blah(), 5) { //do something
What does the following code do? A link to something in the PHP manual
Why does the following behave unexpectedly in Python? >>> a = 256 >>> b
What does the following error mean? Geeneration of designer file failed: Exception from HRESULT:
Why does the following code print ‘read(): Resource temporarily unavailable’ 80% of the time?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.