Skip to content

Instantly share code, notes, and snippets.

@mollyporph
Last active September 16, 2015 08:52
Show Gist options
  • Save mollyporph/f24c0b45d3193363729c to your computer and use it in GitHub Desktop.
Save mollyporph/f24c0b45d3193363729c to your computer and use it in GitHub Desktop.
Indexed linq list
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