请问下面的一般方法在.NET 4.0框架已经存在?框架、存在、方法、NET

2023-09-06 23:15:37 作者:不回消息的钉子户

执行以下操作泛型函数存在于.NET 4.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(); 
  } 
} 

它允许下面的结构(即嵌入式LINQ查询的SELECT子句中的lambda EX pressions):

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);});

更新:为所有的人质疑这一解决方案,看看Onkelborg的回答,看看它会采取包括但Lambda.Wrap拉姆达()(同时仍保持查询语法)。请不要清除拉姆达。这为讨取 abitrary(返回值)的lambda 。另外,我要寻找一个查询语法解决方案,只。请不转换前pression以流利的语法,从而轻视它。

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.

推荐答案

您可以使用语法在code:

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

另外,您可以使用特殊语法的LINQ扩展方法,而不是直接:

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); });

由于议题更新:

我的意思并不是不尊重,但我认为这个解决方案是没有必要的。

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

下面是一个有点探险的:

Here's a bit of an exploration:

如果我们要接受真正的任意的lambda表达式像你说的,那么他们可以来自外部来源,而总结不做任何事,因为它是一样的 F()

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

但是,如果你这样做, F 不能依靠取值以任何方式(例如,该你可以不写你的榜样方式code):

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

我们真正期待的是任意关闭了取值。为了做到这一点,在lambda表达式必须内嵌定义,其中取值在范围上,这样你就不会真正接受真正的任意的lambda表达式更多,他们有直接在code写的。

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! :)

另外,你可以只使用lambda表达式内搭像 F2 以上参数。

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

如果你真的想坚持使用lambda语法,我建议使用的扩展方法。就像我在我的评论说,它看起来像你正在寻找的东西中途查询和放大器之间;扩展语法。

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.

我很感兴趣,为什么你要使用的lambda语法与查询语法?

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

希望这有助于:)