LINQ的注意事项 [英] Linq Caveats

查看:152
本文介绍了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'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);
}

这篇关于LINQ的注意事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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