方法链可以称为LINQ吗? [英] Can a method chain be called LINQ?
问题描述
将此代码段称为LINQ(语言集成查询)是否正确?
Is it correct to call this code snippet LINQ (Language Integrated Query)?
var lstMyStrings = new List<string>();
lstMyStrings.Where(aX => string.IsNullOrEmpty(aX))
.Skip(3)
.ToList();
我很困惑,因为对于此代码,必须 System.Linq
.
但是,当我看到这样的问题和答案时: .NET LINQ查询语法与方法链
,那么他们在谈论方法链而不是LINQ.
I am confused because System.Linq
is mandatory for this code.
However, when I see questions and answer's like this: .NET LINQ query syntax vs method chain
, then they're talking explicit about a method chain and not LINQ.
推荐答案
LINQ可以用两种不同的方式编写.
一种是通过使用LINQ声明性查询语法编写查询:
LINQ can be written in two different ways.
One is by writing a query using LINQ declarative query syntax:
var query = from x in source
where condition
select x.Property
另一个是通过使用LINQ的扩展方法:
And the other is by using LINQ's extension methods:
var query = source.Where(condition).Select(x => x.Property);
这两个查询是相同的,并且将产生相同的结果(嗯,在这个过于简化的示例中,编译器错误,但是考虑到这一点:-))
Both queries are identical and will produce the same result (well, compiler error in this over-simplified example but it's the thought that counts :-))
c#编译器将查询转换为方法调用.
这意味着您还可以使用方法链来编写您作为查询编写的所有内容.但是请注意,相反的说法是错误的-某些查询只能使用Linq的扩展方法编写.
The c# compiler translates the query into method calls.
This means that everything you write as a query can be also written using method chains. Please note, however, that the opposite is false - Some queries can only be written using Linq's extension methods.
要进一步阅读,请请注意第二段以此开头:
Note the second paragraph starts with this:
查询语法和方法语法在语义上是相同的,但是许多人发现查询语法更简单,更易于阅读.
Query syntax and method syntax are semantically identical, but many people find query syntax simpler and easier to read.
btw,如果尚不清楚,则System.Linq
是方法链接语法必需的原因也是因为linq扩展方法属于该命名空间.
btw, if it was'nt already clear, the reason that System.Linq
is mandatory for the method chaining syntax also is because the linq extension methods belongs to this namespace.
这篇关于方法链可以称为LINQ吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!