显示索引

来源:互联网 发布:网络主播用英语怎么说 编辑:程序博客网 时间:2024/06/05 14:54

LINQ中的很多函数都有对应的包含索引的重载版本,例如:下面的代码

                var authors = new List<Author>()                {                    new Author() { FirstName = "Fabric", LastName = "Marguarie" },                     new Author() { FirstName = "Steve", LastName = "Eichert" },                    new Author() { FirstName = "Jim", LastName = "Wooley" },                     new Author() { FirstName = "Jon", LastName = "Skeet" },                     new Author() { FirstName = "Tom", LastName = "Steve" }                };                 var books = new List<Book>()                {                    new Book() { Name = "LINQ in action", Authors = new List<Author>()                    {                        authors[0], authors[1], authors[2]                    }},                     new Book() { Name = "C# in depth", Authors = new List<Author>()                    {                        authors[3]                    }},                    new Book() { Name = "LINQ internal", Authors = new List<Author>()                    {                        authors[0], authors[4]                    }},                 };                var results = books                    .Where((book, index) => index == 1)                    .Select((book, index) => new { Index = index, book.Name });                 foreach (var result in results)                {                    Console.WriteLine("{0} {1}", result.Index, result.Name);                 }Output:0 C# in depth


原创粉丝点击