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

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

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


当前回答

我知道延迟执行的概念现在应该被灌输给我了,但这个例子确实帮助我实际掌握了它:

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);
    }
}

上面的代码返回如下:

Before add:
Bob
Alice
Trent

After add:
Bob
Alice
Trent
Mallory

其他回答

我知道延迟执行的概念现在应该被灌输给我了,但这个例子确实帮助我实际掌握了它:

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);
    }
}

上面的代码返回如下:

Before add:
Bob
Alice
Trent

After add:
Bob
Alice
Trent
Mallory

我认为关于LINQ to SQL的第一个误解是,你仍然必须了解SQL才能有效地使用它。

关于Linq to Sql的另一个误解是,为了让它工作,你仍然必须降低数据库的安全性到荒谬的地步。

第三点是将Linq to Sql与动态类一起使用(意味着类定义是在运行时创建的)会导致大量的即时编译。这绝对会破坏性能。

事务(不使用TransactionScope)

理解Linq提供者之间的抽象何时泄漏。有些东西适用于对象,但不适用于SQL(例如,. takewhile)。一些方法可以被翻译成SQL (ToUpper),而另一些则不能。有些技术在对象中更有效,而其他技术在SQL中更有效(不同的连接方法)。

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.