Last active
September 16, 2015 08:52
-
-
Save mollyporph/f24c0b45d3193363729c to your computer and use it in GitHub Desktop.
Indexed linq list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
//Create a list of 20 anon objects wiht Name and Age | |
var myListOfObjects = Enumerable.Range(0, 20) | |
.Select(x => new {Name="someName",Age = 5}); | |
//Create indexes | |
var myListOfObjectsAndIndexes = myListOfObjects.Select((item, index) => new { item, index }); | |
//Grab one of the items | |
var OneOfTheItems = myListOfObjectsAndIndexes.ElementAt(5); | |
//Print it | |
Console.WriteLine(String.Format("Index: {0}, Name: {1}, Age: {2}", | |
OneOfTheItems.index, | |
OneOfTheItems.item.Name, | |
OneOfTheItems.item.Age)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment