What's the hardest or most misunderstood aspect of LINQ?
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
).
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#
compiler treats query expressions IQueryable
延迟执行
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("nAfter add:");
foreach (var result in results)
{
Console.WriteLine(result);
}
}
The above code returns the following:
Before add:
Bob
Alice
Trent
After add:
Bob
Alice
Trent
Mallory
除了LINQ
to SQL
,这些功能不仅仅是嵌入在语言中的SQL
解析器。
上一篇: LINQ OrderBy具有多个字段
下一篇: 什么是最困难或最误解的LINQ的方面?