背景:在接下来的一个月里,我将做三次关于LINQ的演讲,或者至少将LINQ包含在c#的上下文中。我想知道哪些话题值得花相当多的精力,这取决于人们可能很难理解哪些话题,或者他们可能有错误的印象。我不会具体讨论LINQ to SQL或实体框架,只是作为如何使用表达式树(通常是IQueryable)远程执行查询的示例。

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

c#编译器如何处理查询表达式 Lambda表达式 表达式树 扩展方法 匿名类型 这个IQueryable 延迟执行与立即执行 流与缓冲执行(例如,OrderBy被延迟但被缓冲) 隐式类型局部变量 读取复杂的泛型签名(例如Enumerable.Join)


当前回答

我想说,LINQ最容易被误解(或者不应该被理解?)的方面是IQueryable和自定义LINQ提供者。

我已经使用LINQ有一段时间了,完全适应IEnumerable世界,并且可以用LINQ解决大多数问题。

但是当我开始看和阅读IQueryable,表达式和自定义linq提供程序时,它让我头晕目眩。如果您想了解一些相当复杂的逻辑,可以看看LINQ to SQL是如何工作的。

我期待了解LINQ的这一方面……

其他回答

执行查询时var代表什么?

它是iQueryable, iSingleResult, iMultipleResult,还是根据实现改变。在c#中使用(似乎是)动态类型与标准静态类型之间存在一些猜测。

延迟加载。

我想说,LINQ最容易被误解(或者不应该被理解?)的方面是IQueryable和自定义LINQ提供者。

我已经使用LINQ有一段时间了,完全适应IEnumerable世界,并且可以用LINQ解决大多数问题。

但是当我开始看和阅读IQueryable,表达式和自定义linq提供程序时,它让我头晕目眩。如果您想了解一些相当复杂的逻辑,可以看看LINQ to SQL是如何工作的。

我期待了解LINQ的这一方面……

编译查询

The fact that you can't chain IQueryable because they are method calls (while still nothing else but SQL translateable!) and that it is almost impossible to work around it is mindboggling and creates a huge violation of DRY. I need my IQueryable's for ad-hoc in which I don't have compiled queries (I only have compiled queries for the heavy scenarios), but in compiled queries I can't use them and instead need to write regular query syntax again. Now I'm doing the same subqueries in 2 places, need to remember to update both if something changes, and so forth. A nightmare.

Explain why Linq does not handle left outer join as simple as in sql syntax. See this articles: Implementing a Left Join with LINQ, How to: Perform Left Outer Joins (C# Programming Guide) I got so disappointed when I came across this obstacle that all my respect for the language vanished and I decedid that it was just something that quickly would fade away. No serious person would want to work with a syntax that lacks these battlefield proven primitives. If you could explain why these sort of set operation are not supported. I would become a better and more openminded person.