我今天偶然发现了这段代码,但是我完全不理解它。
someArray.Select((x, i) => new XElement("entry",
new XElement("field", new XAttribute("name", "Option"), i + 1)
(x, i)
的作用是什么?我看到了对i
的引用,但不明白x
如何适配该lambda表达式。
还有,为什么i
是整数?我注意到末尾有i+1
,因此我认为这是正确的。
感谢您的帮助。
i + 1
... 好吧,我看到了。 - Sam Axex
而不是i
。 - Kellen StuartEnumerable.Range(0, someArray.Length).Select(i => ...)
。这取决于上下文 - 你接下来要用XML元素列表做什么?你能问一下最初编写它的人吗? - Rupi
。唯一提供i
参数的Select
重载方法(参见链接 https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=netframework-4.8#System_Linq_Enumerable_Select__2_System_Collections_Generic_IEnumerable___0__System_Func___0_System_Int32___1__)同时也提供了参数x
。因此,即使代码没有使用x
,也必须声明x
参数。 - GSerg