什么是LINQ的最难的或最被人误解的方面? [英] What's the hardest or most misunderstood aspect of LINQ?

查看:175
本文介绍了什么是LINQ的最难的或最被人误解的方面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:在接下来的一个月,我会给出约三举行会谈或至少包括 LINQ C#。我想知道哪些主题是值得给予关注有相当多的基础上,什么人可能会发现难以理解,或他们可能有一个错误的IM pression。我不会具体谈 LINQ SQL 或实体框架除外的查询是如何执行的例子远程使用前pression树(通常的IQueryable )。

Background: Over the next month, I'll be giving three talks about or at least including LINQ in the context of C#. I'd like to know which topics are worth giving a fair amount of attention to, based on what people may find hard to understand, or what they may have a mistaken impression of. I won't be specifically talking about LINQ to SQL or the Entity Framework except as examples of how queries can be executed remotely using expression trees (and usually IQueryable).

那么,你有什么发现很难约 LINQ ?你有什么误解方面见过?例子可能是以下任意一种,但请不要限制自己!

So, what have you found hard about LINQ? What have you seen in terms of misunderstandings? Examples might be any of the following, but please don't limit yourself!


  • 如何 C#编译器将查询前pressions

  • LAMBDA前pressions

  • 防爆pression树木

  • 扩展方法

  • 匿名类型

  • 的IQueryable

  • 递延VS立即执行

  • 流VS缓冲执行(例如的OrderBy被推迟,但缓冲)

  • 隐式类型的局部变量

  • 阅读复杂的通用符号(如 Enumerable.Join

  • How the C# compiler treats query expressions
  • Lambda expressions
  • Expression trees
  • Extension methods
  • Anonymous types
  • IQueryable
  • Deferred vs immediate execution
  • Streaming vs buffered execution (e.g. OrderBy is deferred but buffered)
  • Implicitly typed local variables
  • Reading complex generic signatures (e.g. Enumerable.Join)

推荐答案

我知道,延迟执行的概念现在应该打成了我的,但这个例子确实帮助我得到它的实际把握:

I know the deferred execution concept should be beaten into me by now, but this example really helped me get a practical grasp of it:

static void Linq_Deferred_Execution_Demo()
{
    List<String> items = new List<string> { "Bob", "Alice", "Trent" };

    var results = from s in items select s;

    Console.WriteLine("Before add:");
    foreach (var result in results)
    {
        Console.WriteLine(result);
    }

    items.Add("Mallory");

    //
    //  Enumerating the results again will return the new item, even
    //  though we did not re-assign the Linq expression to it!
    //

    Console.WriteLine("\nAfter add:");
    foreach (var result in results)
    {
        Console.WriteLine(result);
    }
}

以上code返回如下:

The above code returns the following:

Before add:
Bob
Alice
Trent

After add:
Bob
Alice
Trent
Mallory

这篇关于什么是LINQ的最难的或最被人误解的方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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