C# LINQ 最难或最容易被误解的方面是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/215548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:27:39  来源:igfitidea点击:

What's the hardest or most misunderstood aspect of LINQ?

c#linqc#-3.0

提问by Jon Skeet

Background: Over the next month, I'll be giving three talks about or at least including LINQin 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 LINQto SQLor the Entity Framework except as examples of how queries can be executed remotely using expression trees (and usually IQueryable).

背景:在接下来的一个月,我会给出约或至少包括三名会谈LINQ的背景下C#。我想知道哪些主题值得给予相当多的关注,基于人们可能觉得难以理解的内容,或者他们可能有错误的印象。我不会特别谈论LINQSQL或实体框架除了作为如何查询可以远程使用表达式树(通常被执行的例子IQueryable)。

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!

那么,你发现了什么困难LINQ?你在误解方面看到了什么?示例可能是以下任何一个,但请不要限制自己!

  • 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)
  • C#编译器如何处理查询表达式
  • Lambda 表达式
  • 表达式树
  • 扩展方法
  • 匿名类型
  • IQueryable
  • 延迟执行 vs 立即执行
  • 流式与缓冲执行(例如,OrderBy 被延迟但已缓冲)
  • 隐式类型的局部变量
  • 读取复杂的通用签名(例如Enumerable.Join

采纳答案by JaredPar

Delayed execution

延迟执行

回答by Ryan Eastabrook

Lazy Loading.

懒加载。

回答by smaclell

That there is more than just LINQto SQLand the features are more than just a SQLparser embedded in the language.

不仅仅是LINQ为了SQL和功能不仅仅是SQL嵌入在语言中的解析器。

回答by Tim Jarvis

I think the fact that a Lambdaexpression can resolve to both an expression tree and an anonymous delegate, so you can pass the same declarative lambdaexpression to both IEnumerable<T>extension methods and IQueryable<T>extension methods.

我认为Lambda表达式可以解析为表达式树和匿名委托,因此您可以将相同的声明性lambda表达式传递给IEnumerable<T>扩展方法和IQueryable<T>扩展方法。

回答by user21582

I think you should give more attention to the most commonly used features of LINQ in detail - Lambda expressions and Anonymous types, rather than wasting time on "hard to understand" stuff that is rarely used in real world programs.

我认为你应该更多地关注 LINQ 最常用的特性——Lambda 表达式和匿名类型,而不是在现实世界程序中很少使用的“难以理解”的东西上浪费时间。

回答by James Curran

I still have trouble with the "let" command (which I've never found a use for) and SelectMany (which I've used, but I'm not sure I've done it right)

我仍然无法使用“let”命令(我从未发现其用途)和 SelectMany(我使用过,但我不确定我是否做得对)

回答by Aaron Powell

In LINQ to SQL I constantly see people not understanding the DataContext, how it can be used and how it should be used. Too many people don't see the DataContext for what it is, a Unit of Work object, not a persistant object.

在 LINQ to SQL 中,我经常看到人们不了解 DataContext、如何使用它以及如何使用它。太多人没有看到 DataContext 是什么,它是一个工作单元对象,而不是一个持久对象。

I've seen plenty of times where people are trying to singleton a DataContext/ session it/ etc rather than making a new time for each operation.

我见过很多次,人们试图单例一个 DataContext/会话它/等等,而不是为每个操作创建一个新的时间。

And then there's disposing of the DataContext before the IQueryable has been evaluated but that's more of a prople with people not understanding IQueryable than the DataContext.

然后在对 IQueryable 进行评估之前处理 DataContext ,但这更像是人们不理解 IQueryable 而不是 DataContext 的问题。

The other concept I see a lot of confusion with is Query Syntax vs Expression Syntax. I will use which ever is the easiest at that point, often sticking with Expression Syntax. A lot of people still don't realise that they will produce the same thing in the end, Query is compiled into Expression after all.

我看到很多混淆的另一个概念是查询语法与表达式语法。我将使用当时最简单的方法,通常坚持使用表达式语法。很多人还没有意识到他们最终会产生同样的东西,Query毕竟被编译成Expression。

回答by denis phillips

Understanding when the abstraction among Linq providers leaks. Some things work on objects but not SQL (e.g., .TakeWhile). Some methods can get translated into SQL (ToUpper) while others can't. Some techniques are more efficient in objects where others are more effective in SQL (different join methods).

了解 Linq 提供程序之间的抽象何时泄漏。有些东西对对象有效,但对 SQL 无效(例如,.TakeWhile)。某些方法可以转换为 SQL (ToUpper),而其他方法则不能。有些技术在对象中更有效,而其他技术在 SQL 中更有效(不同的连接方法)。

回答by Robert Rossney

I for one would sure like to know if I need to know what expression trees are, and why.

我肯定想知道我是否需要知道什么是表达式树,以及为什么。

回答by Mark Heath

I'm fairly new to LINQ. Here's the things I stumbled over in my first attempt

我对 LINQ 还很陌生。这是我第一次尝试时偶然发现的事情

  • Combining several queries into one
  • Effectively debugging LINQ queries in Visual Studio.
  • 将多个查询合并为一个
  • 在 Visual Studio 中有效调试 LINQ 查询。