I often have similar constructions:
var t = from i in Enumerable.Range(0,5)
select num(i);
Console.WriteLine(t.Count());
foreach (var item in t)
Console.WriteLine(item);
In this case LINQ will evaluate num() function twice for each element (one for Count() and one for output). So after such LINQ calls I have to declare new vatiable: var t2 = t.ToList();
Is there a better way to do this?
You can call
ToListwithout making a separate variable:EDIT: Or,
Or even