LINQ的注意事项注意事项、LINQ

由网友(得不到的就毁灭)分享简介:LINQ的是一个真棒除了.NET和我发现它使我受益匪浅,在许多情况下,即使我只有开始学习如何使用LINQ。Linq is an awesome addition to .NET and I've found it has served me well in many situations even though I...

LINQ的是一个真棒除了.NET和我发现它使我受益匪浅,在许多情况下,即使我只有开始学习如何使用LINQ。

Linq is an awesome addition to .NET and I've found it has served me well in many situations even though I'm only beginning to learn about how to use Linq.

不过,在阅读我一直在做关于LINQ中,我发现有一些细微的东西开发者需要留意的,能够带来麻烦。

However, in the reading I've been doing about Linq, I've discovered that there are some subtle things a developer needs to keep an eye out for that can lead to trouble.

我已经包括了,我已经遇到一个明确的警告是延迟执行的结果。

I've included one definite caveat that I've come across that is a result of deferred execution.

所以,我不知道还有什么其他注意事项对LINQ存在开发新的Linq应该知道的?

So I'm wondering, what other caveats exist for Linq that developers new to Linq should know about?

推荐答案

在一个foreach循环建立一个查询

Building up a query within a foreach loop

IEnumerable<char> query = "Not what you might expect";
foreach(char vowel in "aeiou")
{
    query = query.Where(c => c != vowel);
}

以上code仅删除了U的字符串,因为延迟执行。

The above code only removes the "u" from the string because of deferred execution.

为了删除所有你需要做以下的元音:

In order to remove all the vowels you need to do the following:

IEnumerable<char> query = "Not what you might expect";
foreach(char vowel in "aeiou")
{
    char temp = vowel;
    query = query.Where(c => c != temp);
}
阅读全文

相关推荐

最新文章