C# LINQ和可枚举。其中(IEnumerable,Func)重载方法

C# LINQ和可枚举。其中(IEnumerable,Func)重载方法,c#,linq,C#,Linq,我有一个关于LINQ中一个棘手问题的问题,这个问题对我来说几乎是棘手的 可以编写以下linqQuery string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var linqQuery= digits.Where((digit, index) => digit.Length < index); ? 方法Enumerable.Wher

我有一个关于LINQ中一个棘手问题的问题,这个问题对我来说几乎是棘手的

可以编写以下linqQuery

string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var linqQuery= digits.Where((digit, index) => digit.Length < index);
?

方法Enumerable.WhereIEnumerable,Func使用Int32参数作为源元素的索引,我想知道是否可以从查询语法而不是其他Enumerable重载方法Enumerable推断出此方法。Where方法IEnumerable,Func

这里是MSDN参考资料


不,无法使用查询语法调用Where重载或类似的Select重载

根据C4.0规范§7.16.2.4:

带有where子句的查询表达式

翻译成

from x in ( e ) . Where ( x => f )
…

不,无法使用查询语法调用Where重载或类似的Select重载

根据C4.0规范§7.16.2.4:

带有where子句的查询表达式

翻译成

from x in ( e ) . Where ( x => f )
…

不,不可能。查询语法仅支持可用操作的子集。查询语法中的任何内容都不会编译到Where重载

你所拥有的一切都很好。下面是一些查询语法的想法,可以让您编写这样的操作:

var linqQuery = from d in digits.Where((digit, index) => digit.Length < index)
                // You can do more stuff here if you like
                select d;

还考虑“SimtTimeBaby”,这就避免了向元组投射的麻烦:

var linqQuery = from tuple in digits.AsSmartEnumerable()
                where tuple.Value.Length < tuple.Index
                // You can do more stuff here if you like
                select tuple.Value;

不,不可能。查询语法仅支持可用操作的子集。查询语法中的任何内容都不会编译到Where重载

你所拥有的一切都很好。下面是一些查询语法的想法,可以让您编写这样的操作:

var linqQuery = from d in digits.Where((digit, index) => digit.Length < index)
                // You can do more stuff here if you like
                select d;

还考虑“SimtTimeBaby”,这就避免了向元组投射的麻烦:

var linqQuery = from tuple in digits.AsSmartEnumerable()
                where tuple.Value.Length < tuple.Index
                // You can do more stuff here if you like
                select tuple.Value;