是LINQ的让关键字比它更好的为关键字? [英] Is linq's let keyword better than its into keyword?
问题描述
目前,我正在刷牙的LINQ,我试图理解让
,并使用到
关键字。到目前为止,让
关键字似乎比将更好
关键字至于我的理解去。
I'm currently brushing up on LINQ and am trying to comprehend the difference between the let
and using the into
keyword. So far the let
keyword seems better than the into
keyword as far as my understanding goes.
到
关键词基本上可以让一个投影后,继续查询。 (只是想明确说明,我不是指一个组参加)
The into
keyword essentially allows one to continue a query after a projection. (Just want to state explicitly that I'm not referring to the one for group join.)
由于名称的数组它允许一个做到以下几点:
Given an array of names it allows one to do the following:
var intoQuery =
from n in names
select Regex.Replace(n, "[aeiou]", "")
into noVowel
where noVowel.Length > 2
select noVowel;
这需要选择并将其放入了 noVowel 结果code>变量,然后允许一个引入额外的
,其中
,排序依据
和选择
条款。一旦 noVowel
变量被创建时, N
变量不再可用。
It takes the result of the select and places it into the noVowel
variable which then allows one to introduce additional where
, orderby
, and select
clauses. Once the noVowel
variable is created, the n
variable is no longer available.
让
关键字,在另一方面,使用温度匿名类型,以便您可以重用多个变量在一个时间。
The let
keyword, on the other hand, uses temp anonymous types to allow you to reuse more than one variable at a time.
您可以执行以下操作:
var letQuery =
from n in names
let noVowel = Regex.Replace(n, "[aeiou]", "")
where noVowel.Length > 2
select noVowel;
无论是 noVowel
和<$ C $ ç> N 变量可使用(尽管我没有在这种情况下使用它)。
Both the noVowel
and n
variables are available for use (even though I haven't used it in this case).
虽然我可以看到的差别,我不明白为什么人会愿意使用到
关键字在让
关键字,除非我们明确地希望确保以前的变量无法在查询的部分后者被使用。
While I can see the difference, I can't quite understand why one would want to use the into
keyword over the let
keyword unless one explicitly wanted to make sure that previous variables were not able to be used in latter parts of the query.
那么,有没有一个很好的理由,为什么这两个关键字的存在?
So, is there a good reason why both keywords exist?
推荐答案
是的,因为他们在做不同的事情,正如你所说的。
Yes, because they're doing different things, as you've said.
SELECT ... INTO
有效地隔离了整个一个查询,并让你用它作为输入到一个新的查询。我个人一般的喜欢通过两个变量来做到这一点:
select ... into
effectively isolates the whole of one query and lets you use it as the input to a new query. Personally I usually prefer to do this via two variables:
var tmp = from n in names
select Regex.Replace(n, "[aeiou]", "");
var noVowels = from noVowel in tmp
where noVowel.Length > 2
select noVowel;
(诚然,在这种情况下,我会用点号做两行,但忽略了.. 。)
(Admittedly in this case I would do it with dot notation in two lines, but ignoring that...)
通常你不会的希望的查询的前面部分的全部行李 - 这是当你使用 SELECT ... INTO
或两按照上面的例子拆分查询。这不仅意味着不能使用时,他们不应该查询的前面部分,它简化了这是怎么回事 - 当然这意味着有可能少复制在每一步怎么回事
Often you don't want the whole baggage of the earlier part of the query - which is when you use select ... into
or split the query in two as per the above example. Not only does that mean the earlier parts of the query can't be used when they shouldn't be, it simplifies what's going on - and of course it means there's potentially less copying going on at each step.
在另一方面,当你的做的要保留现场的其他信息,让
更有意义
On the other hand, when you do want to keep the rest of the context, let
makes more sense.
这篇关于是LINQ的让关键字比它更好的为关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!