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

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

问题描述

背景:在接下来的一个月里,我将在 C# 的上下文中进行三场关于或至少包括 LINQ 的演讲.我想知道哪些主题值得给予相当多的关注,基于人们可能觉得难以理解的内容,或者他们可能有错误的印象.我不会专门讨论 LINQSQL 或实体框架,除了作为如何使用表达式树(通常是 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# 编译器如何处理查询表达式
  • Lambda 表达式
  • 表达式树
  • 扩展方法
  • 匿名类型
  • IQueryable
  • 延迟执行与立即执行
  • 流式与缓冲执行(例如,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("
After add:");
    foreach (var result in results)
    {
        Console.WriteLine(result);
    }
}

以上代码返回如下:

Before add:
Bob
Alice
Trent

After add:
Bob
Alice
Trent
Mallory

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

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