LINQ查询 - 需要解释为什么这些例子都不同 [英] LINQ Query - Explanation needed of why these examples are different

查看:73
本文介绍了LINQ查询 - 需要解释为什么这些例子都不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读这本书LINQ袖珍参考还有就是我有困难得到我的头左右...
在书中解释一个具体的例子(以下略作修改)是位简单地说,所以我想知道如果是这样,这是有道理的人可以打破它一步一步对我来说...

I'm reading the book "LINQ Pocket Reference" and there is a particular example (slightly modified below) that I'm having difficulty getting my head around... The explanation in the book is a bit brief, so I was wondering if someone could break it down step-by-step for me so that it makes sense...

    IEnumerable<char> query2 = "Not what you might expect";
    foreach (char vowel in "aeiou")
    {
        var t = vowel;
        query2 = query2.Where(c => c != t);
        // iterate through query and output (snipped for brevity)
    }



输出这样的:

Outputs this:


    Not wht you might expect
    Not wht you might xpct
    Not wht you mght xpct
    Nt wht yu mght xpct
    Nt wht y mght xpct

这让我感觉良好......然而,这。不

Which makes perfect sense to me... However, this does not.

    IEnumerable<char> query2 = "Not what you might expect";
    foreach (char vowel in "aeiou")
    {
        query2 = query2.Where(c => c != vowel);
        // iterate through query and output (snipped for brevity)
    }




    Not wht you might expect
    Not what you might xpct
    Not what you mght expect
    Nt what yu might expect
    Not what yo might expect

这不...

有人可以给我一个更好的解释究竟是怎么回事?

Can someone give me a better explanation of exactly what is going on here?

推荐答案

在第一个例子中,会发生什么是元音的数值被捕获到本地(对用户资格的范围循环)变量。

What happens with the first example is that the value of vowel is captured into a local (to the scope of the for-loop) variable.

用于查询的WHERE子句将然后使用该捕获变量。凡从句这样用匿名方法/λ的方法,它可以捕获局部变量。会发生什么情况,然后是,它捕获变量的当前值。

The where-clause for the query will then use that captured variable. Where-clauses like this uses an anonymous method/lambda method, which can capture local variables. What happens then is that it captures the current value of the variable.

在第二类中,但是,它不捕捉当前值,只有可变使用,因此,因为这个变量的变化,每次执行循环,你建的最后一顶新的WHERE子句,但因为你改变变量,你还挺修改所有前面的人也是如此。

In the second class, however, it doesn't capture the current value, only which variable to use, and thus since this variable changes, each time you execute the loop, you build a new Where-clause on top of the last one, but you kinda modify all the preceding ones as well since you change the variable.

因此,在第一个例子,你就会得到这种类型的查询:

So in the first example, you get this type of query:

IEnumerable<char> query2 = "Not what you might expect";
Char t1 = 'a'; query2 = query2.Where(c => c != t1);
Char t2 = 'e'; query2 = query2.Where(c => c != t2);
Char t3 = 'i'; query2 = query2.Where(c => c != t3);
Char t4 = 'o'; query2 = query2.Where(c => c != t4);
Char t5 = 'u'; query2 = query2.Where(c => c != t5);

在第二个例子中,你会得到这样的:

In the second example, you get this:

IEnumerable<char> query2 = "Not what you might expect";
Char vowel = 'a'; query2 = query2.Where(c => c != vowel);
vowel = 'e'; query2 = query2.Where(c => c != vowel);
vowel = 'i'; query2 = query2.Where(c => c != vowel);
vowel = 'o'; query2 = query2.Where(c => c != vowel);
vowel = 'u'; query2 = query2.Where(c => c != vowel);



到时候你执行第二个例子中,元音将是'U',所以只有你也会被清除掉。你有,但是,5环以上相同的字符串剥离出来的'U',但只有课程的第一个会做的。

By the time you execute this second example, the value of vowel will be 'u', so only the u will be stripped out. You have, however, 5 loops over the same string to strip out the 'u', but only the first one will of course do it.

变量此捕获是的事情之一,我们使用匿名方法/ lambda表达式时,你可以在这里阅读更多关于它在所有行程: C#深入:该美容瓶盖

This capturing of variables is one of the things we all trip over when using anonymous methods/lambdas, and you can read more about it here: C# In Depth: The Beauty of Closures.

如果您页浏览到在比较捕获策略文:复杂VS功率,你会发现这种行为的一些例子。

If you browse down that page to the text under Comparing capture strategies: complexity vs power, you'll find some examples of this behaviour.

这篇关于LINQ查询 - 需要解释为什么这些例子都不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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