Skip to content

Instantly share code, notes, and snippets.

@keithbloom
Created October 30, 2011 09:32
Show Gist options
  • Save keithbloom/1325736 to your computer and use it in GitHub Desktop.
Save keithbloom/1325736 to your computer and use it in GitHub Desktop.
AddTheSequenceNumberToALinqQuery
var lines = new[]
{
"Line A",
"Line B",
"Line C"
};
var dict = new Dictionary<string, string>();
for(int i=0; i <= lines.GetUpperBound(0); i++)
{
dict.Add(string.Format("Address {0}", i), lines[i]);
}
foreach (var dictionary in dict)
{
Console.WriteLine("{0} : {1} ",dictionary.Key, dictionary.Value);
}
Key: Address 0 - Value: Line A
Key: Address 1 - Value: Line B
Key: Address 2 - Value: Line C
var lines = new[]
{
"Line A",
"Line B",
"Line C"
};
var query = lines.Select(line => new
{
Prop = string.Format("Address {0}", line.Count()),
Value = line
});
foreach (var prop in query)
{
Console.WriteLine("Prop: {0} - Value: {1} ",prop.Prop, prop.Value);
}
Prop: Address 6 - Value: Line A
Prop: Address 6 - Value: Line B
Prop: Address 6 - Value: Line C
var lines = new[]
{
"Line A",
"Line B",
"Line C"
};
var query = lines.Select((line, index) =>; new
{
Prop = string.Format("Address {0}", index),
Value = line
});
foreach (var prop in query)
{
Console.WriteLine("Prop: {0} - Value: {1} ",prop.Prop, prop.Value);
}
Prop: Address 0 - Value: Line A
Prop: Address 1 - Value: Line B
Prop: Address 2 - Value: Line C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment