Try using let as follows:
int[] ints = new[] { 1, 2, 3, 4, 5 }; int counter = 0; var result = from i in ints where i % 2 == 0 let number = ++counter select new { I = i, Number = number }; foreach (var r in result) { Console.WriteLine(r.Number + ": " + rI); }
I cannot test it with real LINQ to SQL or Entity Framework right now. Please note that the above code will save the counter value between several query executions.
If this is not supported by your specific provider, you can always select foreach (thus forcing the request) and assign the number manually in the code.
source share