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

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

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


当前回答

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.

其他回答

我不知道这是否属于误解,但对我来说,就是未知。

我很高兴了解了DataLoadOptions以及在进行特定查询时如何控制连接哪些表。

更多信息请参见这里:MSDN: DataLoadOptions

如何LINQ到SQL翻译它!

假设我们有一个有3个字段的表;A, B和C(它们是整数,表名为“Table1”)。 我是这样展示的: [a, b, c]

现在我们想要得到这样的结果: [x = a, y = b + c]

我们有这样一门课:

public class Temp
{
   public Temp(int x, int y)
   {
      this.X = x;
      this.Y = y;
   }

   public int X { get; private set; }
   public int Y { get; private set; }
}

然后我们这样使用它:

using (MyDataContext db = new MyDataContext())
{
   var result = db.Table1.Select(row => 
                   new Temp(row.A, row.B + row.C)).ToList();
}

生成的SQL查询是:

SELECT [t0].[A] AS [x], [t0].[B] + [t0].[C] AS [y]
FROM [Table1] AS [t0]

它转换了Temp的.ctor,它知道我想要“row”。B + row.C”(甚至更多…)放在我的类构造函数的“y”参数上!

我对这些译文很感兴趣。我喜欢这样,我认为编写这样的翻译器(LINQ to Something)有点难!

当然!这是一个坏消息:LINQ to Entities(4.0)不支持带参数的构造函数。(为什么不呢?)

我认为人们对LINQ的误解在于它是一种语言扩展,而不是数据库扩展或构造。

LINQ不仅仅是LINQ to SQL。

现在我们大多数人都在集合上使用了LINQ,我们再也不会回去了!

LINQ是。net自2.0的泛型和3.0的匿名类型以来最重要的特性。

现在我们有了Lambda,我等不及要进行并行编程了!

分组还是让我头晕。

关于延迟执行的任何困惑都应该能够通过逐步执行一些简单的基于linq的代码并在观察窗口中进行操作来解决。

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.