linq“让"翻译 [英] linq "let" translation

查看:70
本文介绍了linq“让"翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,当C#编译器看到 linq查询理解时,它基本上可以直接转换为相应的Linq扩展方法和lambda.即

I understand that when the C# compiler sees a linq query comprehension, it basically does a straight translation to the corresponding Linq Extension methods and lambdas. i.e.

from x in list
select x.property

获取翻译为:

list.Select(x => x.property)

我的问题是let子句会被翻译成什么.例如,编译器将如何翻译它.

my question is what do let clauses get translated to. for example how would this get translated by the compiler.

from x in list
let v = SomeComplexExpressionDependingOnx
select v

(附言.我知道这可以简化为select SomeComplexExpressionDependingOnx,但我想知道总体上是如何做到的)

(p.s. i know this could be reduced to just select SomeComplexExpressionDependingOnx but i want to know how this is done in general)

谢谢!

推荐答案

在这种情况下,它会翻译为:

In this particular case, it gets translated to:

list.Select( x => SomeComplexExpressionDependingOnx );

但是可能会有更复杂的情况,例如:

But there may be a more complex case, such as:

from x in list
let v = SomeComplexExpressionDependingOnx
where v > 10 && v+5 < 50 && SomeFunc(v) == "str"
select x

将翻译为:

list.Where( x => 
    {
        var v = SomeComplexExpressionDependingOnx;
        return v > 10 && v+5 < 50 && SomeFunc(v) == "str";
    }
)

换句话说,let关键字是一种最小化和/或优化查询的方法.也就是说,没有let关键字,您将必须编写:

In other words, the let keyword is a way to minimize and/or optimize your query. That is, without the let keyword you would have to write:

from x in list
where
    SomeComplexExpressionDependingOnx > 10 &&
    SomeComplexExpressionDependingOnx+5 < 50 &&
    SomFunc(SomeComplexExpressionDependingOnx) == "str"
select x

可能会对同一表达式进行三重评估.

Resulting in possible triple evaluation of the same expression.

首先,块表达式"有什么可怕的地方?它们只是任意代表的简写.也就是说,下面的表达式:

First, what's so scary about "block expressions"? They're just a shorthand for arbitrary delegate. That is, the following expression:

Func<string,int> f = 
    s =>
    {
        var ln = s.Length;
        return ln/2;
    }

等效于以下内容:

int CompilerGeneratedMethodIdentifier0( string s )
{
    var ln = s.Length;
    return ln/2;
}

...

Func<string, int> f = new Func<string, int>( CompilerGeneratedMethodIdentifier0 );

第二,关于块表达式"的特殊有什么用?您是否知道mmm ...让我们称它们为" non-block "表达式也可以扩展为相同的代码?也就是说,简单代码new Func<string,int>( s => s.Length/2 )绝对等同于:

Second, what's so special about "block expressions"? Did you know that mmm... let's call them "non-block" expressions also expand to the very same code? That is, the simple code new Func<string,int>( s => s.Length/2 ) is absolute equivalent to:

int CompilerGeneratedMethodIdentifier0( string s )
{
    return s.Length/2;
}

...

new Func<string, int>( CompilerGeneratedMethodIdentifier0 );

第三,关于块表达式"的 non-linqy 是什么? LINQ到处都使用委托,而LINQ并不重要,您使用哪种确切的快捷方式来代表这些委托.

Third, what's so non-linqy about "block expressions"? LINQ uses delegates all over the place, and it doesn't really matter to LINQ what exact shortcut you use to represent those delegates.

尤其是,您的表达式from a in list where a.SomeProp > 10 select new { A = a, B = a.GetB() }被翻译为以下内容:

In particular, your expression from a in list where a.SomeProp > 10 select new { A = a, B = a.GetB() } gets translated into the following:

class AnonymousType0
{
    public MyClass A { get; set; }
    public othertype B { get; set; }
}

bool WhereFunc0( MyClass a )
{
    return a.SomeProp > 10;
}

AnonymousType0 SelectResultFunc0( MyClass a )
{
    AnonymousType0 result = new AnonymousType0();
    result.A = a;
    result.B = a.GetB();
    return result;
}

...

list
    .Where( new Func<MyClass,bool>( WhereFunc0 ) )
    .Select( new Func<MyClass,AnonymousType0>( SelectResultFunc0 ) );

第四,要获得这种理解,人们可以玩弄语言并思考.就是要动脑筋.

Fourth, to get understanding like this, one can just play with the language and think. Use one's brain, that is.

第五,,如果先前的建议由于某种原因对您不起作用,则您始终具有

And fifth, if the previous advice doesn't work for you for one reason or another, you always have ILSpy. Very useful tool, everybody should have one.

这篇关于linq“让"翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆