“i”在这个LINQ语句中的价值在哪里?(Where does “i” get its value in this LINQ statement?)

编程入门 行业动态 更新时间:2024-10-26 06:25:25
“i”在这个LINQ语句中的价值在哪里?(Where does “i” get its value in this LINQ statement?)

我对这个选择LINQ语句的行为感到困惑。 在LOOK HERE注释的下方,您可以看到一个精选的LINQ语句。 该select语句位于employees集合中。 因此,它应该只接受x作为输入参数。 出于好奇,我把i传给了代表,这很有效。 当它遍历select时,它首先指定0然后以1递增。结果可以在本文末尾看到。

变量从哪里得到它的价值? 首先,为什么它允许我使用在范围内无处的变量i 。 它不在本地Main方法中的全局范围内。 任何帮助都是理解这个谜。

namespace ConsoleApplication { using System; using System.Collections.Generic; using System.Linq; public class Employee { public int EmployeedId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } class Program { static void Main(string[] args) { var employees = new List<Employee>() { new Employee() { FirstName = "John", LastName = "Doe" }, new Employee() { FirstName = "Jacob", LastName = "Doe" } }; // LOOK HERE... var newEmployees = employees.Select((x, i) => new { id = i, name = x.FirstName + " " + x.LastName }); newEmployees.ToList().ForEach(x => { Console.Write(x.id); Console.Write(" "); Console.WriteLine(x.name); }); Console.ReadKey(); } } }

结果是

0 John Doe 1 Jacob Doe

I'm little perplexed by the behavior of this select LINQ statement. Just below the LOOK HERE comments you can see a select LINQ statement. That select statement is on the employees collection. So, it should accept only x as the input param. Out of curiosity I passed i to the delegate and it works. When it iterates through the select, it assigns 0 first and then it increments by 1. The result can be seen at the end of this post.

Where does the variable i get its value from? First of all, why does it allow me to use a variable i which is nowhere in the scope. It is not in the global scope neither in the local Main method. Any help is appreciated to understand this mystery.

namespace ConsoleApplication { using System; using System.Collections.Generic; using System.Linq; public class Employee { public int EmployeedId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } class Program { static void Main(string[] args) { var employees = new List<Employee>() { new Employee() { FirstName = "John", LastName = "Doe" }, new Employee() { FirstName = "Jacob", LastName = "Doe" } }; // LOOK HERE... var newEmployees = employees.Select((x, i) => new { id = i, name = x.FirstName + " " + x.LastName }); newEmployees.ToList().ForEach(x => { Console.Write(x.id); Console.Write(" "); Console.WriteLine(x.name); }); Console.ReadKey(); } } }

The result is

0 John Doe 1 Jacob Doe

最满意答案

Enumerable.Select有一个重载项,用于投射序列中元素的当前索引。 还有Enumerable.Where和Enumerable.SkipWhile / TakeWhile拥有它。 您可以像for循环中的循环变量一样使用它for这有时很方便。

使用索引创建匿名类型以将长列表分组为4个组的一个示例:

var list = Enumerable.Range(1, 1000).ToList(); List<List<int>> groupsOf4 = list .Select((num, index) => new { num, index }) .GroupBy(x => x.index / 4).Select(g => g.Select(x => x.num).ToList()) .ToList(); // 250 groups of 4

或者只选择偶数索引的Where :

var evenIndices = list.Where((num, index) => index % 2 == 0);

可能还很重要的是,您可以使用仅在方法语法中投影索引的这些重载。 LINQ查询语法不支持它。

Enumerable.Select has an overload that projects the current index of the element in the sequence. Also Enumerable.Where and Enumerable.SkipWhile/TakeWhile have it. You can use it like a loop variable in a for-loop which is sometimes handy.

One example which uses the index to create an anonymous type to group a long list into groups of 4:

var list = Enumerable.Range(1, 1000).ToList(); List<List<int>> groupsOf4 = list .Select((num, index) => new { num, index }) .GroupBy(x => x.index / 4).Select(g => g.Select(x => x.num).ToList()) .ToList(); // 250 groups of 4

or one with Where which only selects even indices:

var evenIndices = list.Where((num, index) => index % 2 == 0);

It might also be important to mention that you can use these overloads that project the index only in method-syntax. LINQ query-syntax does not support it.

更多推荐

本文发布于:2023-08-07 12:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这个   语句   价值   statement   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!